[英]How to import numeric data with 00 as prefix correctly in pandas?
Example my data set has 00501
, 00544
例如我的数据集有00501
, 00544
After importing as a dataframe it becomes as shown below 导入为数据框后,如下图所示
0 501
1 544
Name: VALUE, dtype: int64
Assuming that is zip code , so nchar should equal to 5 假设这是邮政编码,那么nchar应该等于5
df.v.astype(str).str.rjust(5,'0')
Out[101]:
0 00501
1 00544
Name: v, dtype: object
使用zfill
:
df['Col'] = df['Col'].astype(str).str.zfill(5)
就像之前的注释一样,如果您正在从文件data.csv
中读取,则在给定列上的前导0处,您可能会考虑使用
df = pd.read_csv("data.csv", dtype="object")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.