简体   繁体   English

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

[英]Split a pandas column and append the new results to the dataframe

How can I split a pandas column and append the new results to the dataframe? 如何拆分pandas列并将新结果附加到数据框? I also want there to be no white space. 我也希望没有空白区域。

Example of my desired output: 我想要的输出示例:

col1
Smith, John
Smith, John

col2               
Smith
Smith

col3
John
John

I been trying this but the lambda function is not appending the results how I want it to. 我一直在尝试这个但是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])

I end up getting 我最终得到了

col2  col3
Smith Smith
John  John

Use Series.str.split(..., expand=True) : 使用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

We can use Series.str.extract() method: 我们可以使用Series.str.extract()方法:

In [157]: df[['col2','col3']] = df['col1'].str.extract('(\w+),\s*(\w+)', expand=True)

In [158]: df
Out[158]:
                 col1        col2   col3
0         Smith, John       Smith   John
1         Smith, John       Smith   John
2  Mustermann,    Max  Mustermann    Max
3          Last,First        Last  First

(\\w+),\\s*(\\w+) is a RegEx (Regular Expression) explained here (\\w+),\\s*(\\w+)是这里解释的RegEx(正则表达式)

If you just want to store first string after split, then use following 如果您只想在拆分后存储第一个字符串,请使用以下内容

df['col2'] = df['col1'].str.split(',', 1).str[0] 

          col1   col2
0  Smith, John  Smith  
1  Smith, John  Smith  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM