1

I am exporting the data frame below to csv file in Python. The leading 0s are removed in the csv file.

      Name         ID
0     Bob          0245
1     Tina         2G5B

Both Name and ID values are strings in my pandas df.

I have no problem when re-opening the pandas df in Python with leading 0s after the df is saved by specifying dtype = 'str' in pd.read_csv.

However, I would like to open it in R using the read.csv command, the leading 0s will disappear.

If there is a way that I can save the csvs in Python with leading 0s showing up in the csv file. This problem could be solved.

Please advice.

Thanks!

M--
  • 25,431
  • 8
  • 61
  • 93
MAMS
  • 419
  • 1
  • 6
  • 17
  • This might help [How to save a CSV from dataframe, to keep zeros left in column with numbers?](https://stackoverflow.com/questions/48903008/how-to-save-a-csv-from-dataframe-to-keep-zeros-left-in-column-with-numbers). – deepseefan Sep 21 '19 at 15:46

1 Answers1

1

If we are. using R, then also use the colClasses argument from read.csv to read it as character else, otherwise it would automatically pick up the type of the columns from the values and read it as numeric

df1 <- read.csv("file.csv", colClasses = "character")
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thanks guys for your answer! I tried specifying colClasses = "character" in read.csv command when opening it in R. However, the df in R still does not have leading 0s for the values that should have. Is this because there is no leading 0s in the csv saved from Python, and R does not recognize it? – MAMS Sep 21 '19 at 15:37
  • And in ID column, I have both character and numeric values. They are strings when save them in csv in Python. – MAMS Sep 21 '19 at 15:39
  • @MAMS Probably, when you saved the .csv, it may not have. Please check that file – akrun Sep 21 '19 at 15:39
  • @MAMS If the column contains at least a string, it will be by default read as character (also check `stringsAsFactors` in `read.csv` – akrun Sep 21 '19 at 15:41
  • @akrun Yes, the leading 0s are in pandas df in python, but not in the csv saved. Is there a way to keep those 0s in the csv when export using to_csv command? – MAMS Sep 21 '19 at 15:42
  • 1
    I reckon this is not `R`'s problem. As indicated in the OP, the `csvs` don't have them. – deepseefan Sep 21 '19 at 15:43
  • @MAMS. You can check [here](https://stackoverflow.com/questions/17092671/python-pandas-output-dataframe-to-csv-with-integers) – akrun Sep 21 '19 at 15:47
  • 1
    @deepseefan. OP didn't show the `read.csv` code. So, I don't know whether he included `colClasses` or not – akrun Sep 21 '19 at 15:48
  • @akrun I was recounting the last sentences of the OP where it says `... that I can save the csvs in Python with leading 0s showing up ..`. – deepseefan Sep 21 '19 at 15:53
  • 1
    Thanks for your advice guys. Adding a sep argument in to_csv command solved my problem! – MAMS Sep 21 '19 at 16:21