简体   繁体   中英

How to import numeric data with 00 as prefix correctly in pandas?

Example my data set has 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

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM