我有一个 pandas 数据框,其中前 3 列是字符串:
ID text1 text 2
0 2345656 blah blah
1 3456 blah blah
2 541304 blah blah
3 201306 hi blah
4 12313201308 hello blah
我想在 ID 中添加前导零:
ID text1 text 2
0 000000002345656 blah blah
1 000000000003456 blah blah
2 000000000541304 blah blah
3 000000000201306 hi blah
4 000012313201308 hello blah
我努力了:
df['ID'] = df.ID.zfill(15)
df['ID'] = '{0:0>15}'.format(df['ID'])
str
属性包含字符串中的大部分方法。
df['ID'] = df['ID'].str.zfill(15)
查看更多:http://pandas.pydata.org/pandas-docs/stable/text.html http://pandas.pydata.org/pandas-docs/stable/text.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)