拆分pandas列并将新结果附加到数据帧

Opt*_*ime 3 python lambda pandas

如何拆分pandas列并将新结果附加到数据框?我也希望没有空白区域.

我想要的输出示例:

col1
Smith, John
Smith, John

col2               
Smith
Smith

col3
John
John
Run Code Online (Sandbox Code Playgroud)

我一直在尝试这个但是lambda函数并没有将结果附加到我想要的结果.

df_split = df1['col1'].apply(lambda x: pd.Series(x.split(',')))
df1['col2']= df_split.apply(lambda x: x[0])
df1['col3']= df_split.apply(lambda x: x[1])
Run Code Online (Sandbox Code Playgroud)

我最终得到了

col2  col3
Smith Smith
John  John
Run Code Online (Sandbox Code Playgroud)

cs9*_*s95 5

用途Series.str.split(..., expand=True):

df[['col2', 'col3']] = df.col1.str.split(',\s+', expand=True); df

          col1   col2  col3
0  Smith, John  Smith  John
1  Smith, John  Smith  John
Run Code Online (Sandbox Code Playgroud)