Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get second word from string in pandas column

Tags:

python

pandas

I need to get the second word of a sentence stored in a pandas column. I can easily get the first word with the following line:

df['First'] = df['Sentence'].astype(str).apply(lambda x: x.split()[0])

then why in the world trying to get the second word this way fails:

df['Second'] = df['Sentence'].astype(str).apply(lambda x: x.split()[1])

giving me

IndexError: list index out of range
like image 286
famargar Avatar asked Oct 20 '25 08:10

famargar


1 Answers

Use str.split with str[1] and if no second word get NaNs:

df = pd.DataFrame({'Sentence':['a','a d','s df sd']})
df['Second'] = df['Sentence'].astype(str).str.split().str[1]
print (df)
  Sentence Second
0        a    NaN
1      a d      d
2  s df sd     df

Explanation of error:

There is at least one sentence with no space, so selecting second value of list in x.split()[1] raise error, because second list does not exist.

like image 94
jezrael Avatar answered Oct 22 '25 21:10

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!