Series.str.pad(width, side=’left’, fillchar=’ ‘)设置字符串的宽度
参数:
width : 整型,字符串最小的宽度
side : {‘left’, ‘right’, ‘both’}填充的方向
fillchar : 字符串,空白的地方填充的东西
返回值:
序列Series/索引Index
import pandas as pd
s = pd.Series(['A', 'B', 'C', 'Aaba', 'Baca', 'CABA', 'dog', 'cat'])
print(s.str.pad(5,side="left",fillchar="a"))
0 aaaaA
1 aaaaB
2 aaaaC
3 aAaba
4 aBaca
5 aCABA
6 aadog
7 aacat
dtype: object
Series.str.center(width, fillchar=’ ‘)设置字符的宽度,从两边填充,等同于pad(side=”both”)
参数:
width : 整型,最小宽度
fillchar : 字符串,填充内容
返回值:
序列Series/索引Index
import pandas as pd
s = pd.Series(['A', 'B', 'C', 'Aaba', 'Baca', 'CABA', 'dog', 'cat'])
print(s.str.center(5,fillchar="a"))
0 aaAaa
1 aaBaa
2 aaCaa
3 aAaba
4 aBaca
5 aCABA
6 adoga
7 acata
dtype: object
Series.str.ljust(width, fillchar=’ ‘)设置字符的宽度,从两边填充,等同于pad(side=”right”)
参数:
width : 整型,最小宽度
fillchar : 字符串,填充内容
返回值:
序列Series/索引Index
import pandas as pd
s = pd.Series(['A', 'B', 'C', 'Aaba', 'Baca', 'CABA', 'dog', 'cat'])
print(s.str.ljust(5,fillchar="a"))
0 Aaaaa
1 Baaaa
2 Caaaa
3 Aabaa
4 Bacaa
5 CABAa
6 dogaa
7 cataa
dtype: object
Series.str.rjust(width, fillchar=’ ‘)等同于pad(side=”left”)字符串右移
Series.str.zfill(width)等同于pad(with,side=”left”,fillchar=”0”)0填充