Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 向行值添加尾随零以确保有10位数字_Python_Pandas_Dataframe - Fatal编程技术网

Python 向行值添加尾随零以确保有10位数字

Python 向行值添加尾随零以确保有10位数字,python,pandas,dataframe,Python,Pandas,Dataframe,如果我有一个数据帧,其中每行中的最大数字是10,但有些ID小于10,因为尾部的零已被截断,那么如何在python中添加尾部的零以确保每行中有10个数字 ID 1234567689 123456768 12345676 您可以使用str.pad(),我认为它非常适合这种情况: df['ID'] = df['ID'].str.pad(width=10,side='right',fillchar='0') 如果列的dtype不是字符串,则可以首先将其转换为: df['ID'] = df['ID']

如果我有一个数据帧,其中每行中的最大数字是10,但有些ID小于10,因为尾部的零已被截断,那么如何在python中添加尾部的零以确保每行中有10个数字

ID
1234567689
123456768
12345676
您可以使用
str.pad()
,我认为它非常适合这种情况:

df['ID'] = df['ID'].str.pad(width=10,side='right',fillchar='0')
如果列的
dtype
不是字符串,则可以首先将其转换为:

df['ID'] = df['ID'].astype(str).str.pad(width=10,side='right',fillchar='0')
输出:

           ID
0  1234567689
1  1234567680
2  1234567600
另一种方法是使用:

结果:

           ID
0  1234567689
1  1234567680
2  1234567600

您可以使用
ljust
进行以下操作:

df = df['ID'].astype(str).str.ljust(10, '0')
print(df)

0    1234567689
1    1234567680
2    1234567600

我认为f格式可以做到这一点

X=[123456768912345123]

打印([f'{item:0该列的
dtype
是什么?为什么后面的零被截断了?dtype是字符串
           ID
0  1234567689
1  1234567680
2  1234567600
df = df['ID'].astype(str).str.ljust(10, '0')
print(df)

0    1234567689
1    1234567680
2    1234567600