Home > Mobile >  Issue when export to csv pandas
Issue when export to csv pandas

Time:01-19

when export to csv a numeric need all values to be exported as a string including numeric value have value like this 012546320 would be like this 12546320 after exported to csv

need to export all value as a string to avoid this problem

-df.to_csv('hhh.csv') i have tried to path str to float_format but it didn't work, also i search on google and here but didn't find any a solution

-df.to_csv('hhh.csv',float_format='str')

this the data before export to csv enter image description here

this is the data after the 0 is removed, i didn't want to remove it enter image description here

CodePudding user response:

If need remove 0 for all strings columns from left side use:

cols = df.select_dtypes(object).columns
df[cols] = df[cols].apply(lambda x: x.str.lstrip('0'))

CodePudding user response:

Try dtype=str or maybe converting first to exel pd.ExcelWriter would help

CodePudding user response:

I don't understand why you just don't do

df['part'] = df['part'].astype("string")

With a regular export

df.to_csv('hhh.csv')

What happens then?

  • Related