-3

I have a excel file and I read into a dataframe. I'd like to output this df to CSV file. But one of the columns (labeled id) in CSV file are integer like 1 ,but wanna output string like"0001"with leading zeros.
Every time I try to output the file, it interprets this column as integer and removes the leading zeros. I need all leading zeros.

I've tried

import pandas as pd
df = pd.read_excel(<path&filenane>,convertors={"id":object})
df.to_csv(<path&filename>)

CSV file I'd like to see

id,name,birthday
0001,smith,1980/01/01
dtypes =object,object,object

But now

id,name,birthday
1,smith,1980/01/01
dtypes=int,object,object
  • I honestly do not understand what you're asking here, what has input id = '0001' mean here? – EdChum Jan 11 '17 at 16:43
  • Are you saying something like `to-csv` is treating `0001` as an int, but you don't want it to do that? – doctorlove Jan 11 '17 at 16:46
  • Thank you for editing my question and answering!! This question is pandas.to_csv problem.In case of using pandas.to_excel,I'm able to output str values like "0001",but in case of use pandas.to_csv, output int values like "1" not leading zero – user7376948 Jan 11 '17 at 22:53

1 Answers1

0

You can control the type and format of input columns with the converters parameter. The following example accomplishes what you're after.

  • Make sure to use your filename in place of StringIO(txt)
  • The converter parameter takes a dictionary with the key being the name of the column and the value being a callable. In this case we use the string object class as the callable. It will apply this callable ("function" sort of) to the column thus keeping the leading zeros.

import pandas as pd
from io import StringIO

txt = """id,name,birthday
0001,smith,1980/01/01
"""

df = pd.read_csv(StringIO(txt), converters=dict(id=str))

df

     id   name    birthday
0  0001  smith  1980/01/01
piRSquared
  • 285,575
  • 57
  • 475
  • 624
  • OK I re-read the question, I thought the data was an excel sheet but it's a csv, anyway I think this question is a dupe of this: http://stackoverflow.com/questions/13293810/import-pandas-dataframe-column-as-string-not-int – EdChum Jan 11 '17 at 18:42