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
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']]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With