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
Use str.split
with str[1]
and if no second word get NaN
s:
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.
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