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的文件
让我们举个例子:
>>> 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)
输出:
你会发现前导零完好无损..
,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)
| 归档时间: |
|
| 查看次数: |
3741 次 |
| 最近记录: |