gpt4 book ai didi

python - 在 Pandas Dataframe 中为字符串添加前导零

转载 作者:IT老高 更新时间:2023-10-28 21:43:23 26 4
gpt4 key购买 nike

我有一个 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

关于python - 在 Pandas Dataframe 中为字符串添加前导零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23836277/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com