简体   繁体   中英

How to slice pandas column with index list?

I'm try extract the first two words from a string in dataframe

df["Name"]

     Name                            
     Anthony Frank Hawk               
     John Rodney Mullen               
     Robert Dean Silva Burnquis
     Geoffrey Joseph Rowley 

To get index of the second " "(Space) I try this but find return NaN instead return number of characters until second Space.

df["temp"] = df["Name"].str.find(" ")+1
df["temp"] = df["Status"].str.find(" ", start=df["Status"], end=None)
df["temp"]
0   NaN
1   NaN
2   NaN
3   NaN

and the last step is slice those names, I try this code but don't work to.

df["Status"] = df["Status"].str.slice(0,df["temp"])
df["Status"]
0   NaN
1   NaN
2   NaN
3   NaN

expected return

0   Anthony Frank
1   John Rodney
2   Robert Dean
3   Geoffrey Joseph

if you have a more efficient way to do this please let me know??

df['temp'] = df.Name.str.rpartition().get(0)
df

Output

    Name                        temp
0   Anthony Frank Hawk          Anthony Frank
1   John Rodney Mullen          John Rodney
2   Robert Dean Silva Burnquis  Robert Dean Silva
3   Geoffrey Joseph Rowley      Geoffrey Joseph

EDIT

If only first two elements are required in output.

df['temp'] = df.Name.str.split().str[:2].str.join(' ')
df

OR

df['temp'] = df.Name.str.split().apply(lambda x:' '.join(x[:2]))
df

OR

df['temp'] = df.Name.str.split().apply(lambda x:' '.join([x[0], x[1]]))
df

Output

    Name                        temp
0   Anthony Frank Hawk          Anthony Frank
1   John Rodney Mullen          John Rodney
2   Robert Dean Silva Burnquis  Robert Dean
3   Geoffrey Joseph Rowley      Geoffrey Joseph

You can use str.index(substring) instead of str.find, it returns the smallest index of the substring(such as " ", empty space) found in the string. Then you can split the string by that index and reapply the above to the second string in the resulting list.

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