Python 如何在pandas中正确导入前缀为00的数字数据?
示例我的数据集有Python 如何在pandas中正确导入前缀为00的数字数据?,python,pandas,dataframe,Python,Pandas,Dataframe,示例我的数据集有00501,00544 作为数据帧导入后,它将变为如下所示 0 501 1 544 Name: VALUE, dtype: int64 假设这是邮政编码,那么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).st
00501
,00544
作为数据帧导入后,它将变为如下所示
0 501
1 544
Name: VALUE, dtype: int64
假设这是邮政编码,那么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)
从PyviuOS评论中,如果你从文件<代码>数据.CSV 中读取,在给定列中,你有超前的0,你可以考虑使用
读取它。df = pd.read_csv("data.csv", dtype="object")
如果重要的话,它是字符串数据,而不是数字数据。