Reputation: 2375
I have a dataframe in which one the columns contains english words. I want to pass each of the elements in that columns through NLTKs synsets() function. My issue is that synsets() only takes in the a single word at a time.
e.g wordnet.synsets('father')
Now if I have dataframe like:
dc = {'A':[0,9,4,5],'B':['father','mother','kid','sister']}
df = pd.DataFrame(dc)
df
A B
0 0 father
1 9 mother
2 4 kid
3 5 sister
I want to pass column B though synsets() function and have another column that contains its output. I want to do this without iterating through the dataframe.
How do I do that?
Upvotes: 0
Views: 1200
Reputation: 879869
You could use the apply
method:
In [4]: df['C'] = df['B'].apply(wordnet.synsets)
In [5]: df
Out[5]:
A B C
0 0 father [Synset('father.n.01'), Synset('forefather.n.0...
1 9 mother [Synset('mother.n.01'), Synset('mother.n.02'),...
2 4 kid [Synset('child.n.01'), Synset('kid.n.02'), Syn...
3 5 sister [Synset('sister.n.01'), Synset('sister.n.02'),...
However, having a column of lists is usually not a very useful data structure. It might be better to put each synonym in its own column. You can do that by making the callback function return a pd.Series
:
In [29]: df.join(df['B'].apply(lambda word: pd.Series([w.name for w in wordnet.synsets(word)])))
Out[29]:
A B 0 1 2 3 \
0 0 father father.n.01 forefather.n.01 father.n.03 church_father.n.01
1 9 mother mother.n.01 mother.n.02 mother.n.03 mother.n.04
2 4 kid child.n.01 kid.n.02 kyd.n.01 child.n.02
3 5 sister sister.n.01 sister.n.02 sister.n.03 baby.n.05
4 5 6 7 8
0 father.n.05 father.n.06 founder.n.02 don.n.03 beget.v.01
1 mother.n.05 mother.v.01 beget.v.01 NaN NaN
2 kid.n.05 pull_the_leg_of.v.01 kid.v.02 NaN NaN
3 NaN NaN NaN NaN NaN
(I've chosen to display just the name
attribute of each Synset
; you could of course use
df.join(df['B'].apply(lambda word: pd.Series(wordnet.synsets(word))))
if you want the Synset
objects themselves.)
Upvotes: 2