Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove first word if a sentence from pandas data frame column values

I have a data frame like this:

df:
col1      col2
 A        blue berry
 B        nice water bottle

I want to remove first word from the col2 values, the final data frame will look like this:

df1:
col1       col2
 A         berry
 B         water bottle

How to do this in most effective way using pandas

like image 930
Kallol Avatar asked Mar 04 '23 11:03

Kallol


1 Answers

Use split by first whitespace with n=1 and then select second lists by indexing:

df['col2'] = df['col2'].str.split(n=1).str[1]
print (df)
  col1          col2
0    A         berry
1    B  water bottle

Detail:

print (df['col2'].str.split(n=1))
0           [blue, berry]
1    [nice, water bottle]
Name: col2, dtype: object

If performance is important and no missing values convert solution to list comprehension:

df['col2'] = [x.split(maxsplit=1)[1] for x in df['col2']]
like image 111
jezrael Avatar answered Apr 09 '23 07:04

jezrael