Python 3.x 在连接列时调整字符串
我需要创建一个连接的列,但不知道如何调整 字符串。Python 3.x 在连接列时调整字符串,python-3.x,pandas,Python 3.x,Pandas,我需要创建一个连接的列,但不知道如何调整 字符串。 我需要正确调整名称。填充不起作用 df = pd.DataFrame({"Family":['Antares','Delta','Atlas','Falcon'], "Launches":[1,1,4,11]}) df['Family'] = df['Family'] + ' ' + df['Launches'].astype(str) Family Launches
我需要正确调整名称。填充不起作用
df = pd.DataFrame({"Family":['Antares','Delta','Atlas','Falcon'],
"Launches":[1,1,4,11]})
df['Family'] = df['Family'] + ' ' + df['Launches'].astype(str)
Family Launches
0 Antares 1 1
1 Delta 1 1
2 Atlas 4 4
3 Falcon 11 11
I want the output to look like this
Family Launches
0 Antares 1 1
1 Delta 1 1
2 Atlas 4 4
3 Falcon 11 11
您仍然可以使用
pad
df.Family.str.pad(df.Family.str.len().max(),side = 'left',fillchar = ' ')+ ' ' + df['Launches'].astype(str).str.pad(df['Launches'].astype(str).str.len().max(),side = 'left',fillchar = ' ')
Out[474]:
0 Antares 1
1 Delta 1
2 Atlas 4
3 Falcon 11
dtype: object
您仍然可以使用
pad
df.Family.str.pad(df.Family.str.len().max(),side = 'left',fillchar = ' ')+ ' ' + df['Launches'].astype(str).str.pad(df['Launches'].astype(str).str.len().max(),side = 'left',fillchar = ' ')
Out[474]:
0 Antares 1
1 Delta 1
2 Atlas 4
3 Falcon 11
dtype: object
IIUC,使用
.str.rjust
:
df['Family'] = (df['Family'] + ' ' +
df['Launches'].astype(str).str
.rjust(df['Launches'].astype(str)
.str.len().max(),' '))
输出:
0 Antares 1
1 Delta 1
2 Atlas 4
3 Falcon 11
dtype: object
IIUC,使用
.str.rjust
:
df['Family'] = (df['Family'] + ' ' +
df['Launches'].astype(str).str
.rjust(df['Launches'].astype(str)
.str.len().max(),' '))
输出:
0 Antares 1
1 Delta 1
2 Atlas 4
3 Falcon 11
dtype: object
你能告诉我们预期的产量吗?你是说你想让字符串对正吗?你能告诉我们预期的输出吗?你的意思是你想让字符串对正吗?