为什么 pandas 在写入csv时会删除前导零?
问题描述:
我有一个数据框,其中有一列名为"CBG",数字为字符串值.
I have a dataframe that has a column called 'CBG' with numbers as a string value.
CBG acs_total_persons acs_total_housing_units
0 010010211001 1925 1013
1 010030114011 2668 1303
2 010070100043 930 532
当我将其写入csv文件时,开头的'O'被删除:
When I write it to a csv file, the leading 'O' are removed:
combine_acs_merge.to_csv(new_out_csv, sep=',')
>>> CBG: [0: 10010221101, ...]
已经是一个字符串;如何避免在 .csv
文件
It's already a string; how can I keep the leading zero from being removed in the .csv
file
答
Pandas不会删除填充的零.您喜欢在Excel中打开时看到的内容.在诸如notepad ++之类的文本编辑器中打开csv,您将看到它们仍被零填充.
Pandas doesn't strip padded zeros. You're liking seeing this when opening in Excel. Open the csv in a text editor like notepad++ and you'll see they're still zero padded.