Assuming that df['your_column']
is the column you want to preserve, you can use the dtype
argument in read_csv()
:
df.read_csv('temp.csv', dtype={'your_column': str})
If that's not working, are you sure your columns contain strings to begin with? Because here's the behavior I see:
>>> df1 = pd.DataFrame({'a': ['0000', '0000', '0100',]})
>>> df1
a
0 0000
1 0000
2 0100
>>> df1.to_csv('temp.csv', index=False)
>>> df2.read_csv('temp.csv', dtype={'a': str})
>>> df2
a
0 0000
1 0000
2 0100
Maybe your problem isn't on export or import, but on creation.
df = pd.DataFrame({'a': 0000, 0000, 0100]})
This is going to make a dataframe with values 0,0,100
. If you want them to be strings, you need to create them as strings.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…