Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

python 3.x - Pandas DataFrame export to_csv change dtype of columns

Hopefully a simple request.

I'm finding that when I build a DataFrame and set the column datatypes and then export it to csv it is doing a conversion on the datatype of a numerical string to an integer.

Such as a value might be "0000" and the csv ends up with value 0. But I need it to retain the number of characters in the string and save the csv as "0000".

Anyone know of a way to retain the string rather than the converted datatype?

Setting the datatype after import doesn't solve the issue (before anyone tells me I can set it on/after import), as it causes the issue that when converting the integer to a string you have to also configure the leading 0s on every import as well, which is not optimal.

Hoping I'm overlooking something simple.

(EDIT) oh and my export line is just a simple export which is why it might be I'm just not realising the argument that needs to be provided.

df.to_csv("Test.csv", index=False)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...