为什么在写入 csv 时熊猫会删除前导零?

gwy*_*n93 4 python csv string python-3.x pandas

我有一个数据框,它有一个名为“CBG”的列,其中数字作为字符串值。

    CBG             acs_total_persons   acs_total_housing_units
0   010010211001    1925                1013
1   010030114011    2668                1303
2   010070100043    930                 532    
Run Code Online (Sandbox Code Playgroud)

当我将它写入 csv 文件时,前导的“O”被删除:

combine_acs_merge.to_csv(new_out_csv, sep=',')
>>> CBG: [0: 10010221101, ...]
Run Code Online (Sandbox Code Playgroud)

它已经是一个字符串;我怎样才能保持领先零从在被删除.csv的文件

man*_*eds 8

Pandas 不会删除填充的零。您喜欢在 Excel 中打开时看到这一点。在记事本++等文本编辑器中打开 csv,您会看到它们仍然是零填充的。


Kar*_*mar 5

让我们举个例子:

以下是您的示例数据帧:

>>> df
    col1   num
0    One   011
1    two  0123
2  three  0122
3   four  0333
Run Code Online (Sandbox Code Playgroud)

num视为可以转换为str().

>>> df["num"] = df["num"].astype(str)
>>> df.to_csv("datasheet.csv")
Run Code Online (Sandbox Code Playgroud)

输出:

$猫数据表.csv

你会发现前导零完好无损..

,col1,num
0,One,011
1,two,0123
2,three,0122
3,four,0333
Run Code Online (Sandbox Code Playgroud)

或者,如果您先从 csv 读取数据,则使用 belwo..

pd.read_csv('test.csv', dtype=str)
Run Code Online (Sandbox Code Playgroud)

但是,如果您的专栏CBG已经,str那么它应该是直截了当的。

>>> df = pd.DataFrame({'CBG': ["010010211001", "010030114011", "010070100043"],
...                    'acs_total_persons': [1925, 2668, 930],
...                    'acs_total_housing_units': [1013, 1303, 532]})
>>>
>>> df
            CBG  acs_total_housing_units  acs_total_persons
0  010010211001                     1013               1925
1  010030114011                     1303               2668
2  010070100043                      532                930
>>> df.to_csv("CBG.csv")
Run Code Online (Sandbox Code Playgroud)

结果:

$ cat CBG.csv
,CBG,acs_total_housing_units,acs_total_persons
0,010010211001,1013,1925
1,010030114011,1303,2668
2,010070100043,532,930
Run Code Online (Sandbox Code Playgroud)