为什么 pandas 在写入csv时会删除前导零? [英] Why does pandas remove leading zero when writing to a csv?
本文介绍了为什么 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.
这篇关于为什么 pandas 在写入csv时会删除前导零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文