简体   繁体   中英

Extract everything to the left 2nd occurance of a character in pandas dataframe

I have the following dataframe column:

df: 
     Text
0 J Smith abc def
1 T Smith hij klm
2 A Smith xy z

I am hoping to extract everything before the 2nd space as a new column:

expected output:

    Text               Name
0 J Smith abc def       J Smith
1 T Smith hij klm       T Smith
2 A Smith xy z          A Smith

I have tried the following (some items in Text may be blank hence the if statement):

df['Name'] = df.Text.apply(lambda x: x.split(" ")[0:1]  if len(x) >0 else 0 )

But no such luck. Any ideas on how to get this working?

Use Series.str.split then grab the first 2 elements from the list with str[:2] and then join the strings back together with str.join :

df["Name"] = df["Text"].str.split(" ").str[:2].str.join(" ")

              Text     Name
0  J Smith abc def  J Smith
1  T Smith hij klm  T Smith
2     A Smith xy z  A Smith

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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