[英]How do I add a dynamic number of white spaces to the beginning of each row of a pandas dataframe?
How would I pad each row of the df to the left with enough spaces to ensure the row totals 30 characters? 如何将df的每一行填充到左侧,并留有足够的空格以确保行总计30个字符?
I tried going down this path, but it didn't work and I believe ljust
is deprecated: 我试着走这条路,但它没有工作,我相信
ljust
被弃用:
'{: <30}'.format(df['test1'])
Currently: 目前:
>>> df['test1']
0 1234jjjjjjjdddd
1 1234jjjjjjjdd
2 1234jjjjjjjddddddddd
3 1234jjjjjjjddd
4 1234jjjjjjjdddddd
What I'm looking for: 我在找什么:
>>> df['test1']
0 1234jjjjjjjdddd
1 1234jjjjjjjdd
2 1234jjjjjjjddddddddd
3 1234jjjjjjjddd
4 1234jjjjjjjdddddd
Final Solution: 最终解决方案
df['test1'] = df.test1.str.rjust(30,' ')
This should work: 这应该工作:
spaces = " " * 30
df['test1'] = [spaces[-len(i):] + i for i in df['test1']]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.