Skip to content Skip to sidebar Skip to footer

Unnest Grab Keywords/nextwords/beforewords Function

Background I have the following code to create a df: import pandas as pd word_list = ['crayons', 'cars', 'camels'] l = ['there are many different crayons in the bright blue box and

Solution 1:

Look like unnesting , so we can using

s=df.set_index(['Text']).stack()
s=pd.DataFrame(s.tolist(),index=s.index).stack()
s.apply(lambda x : ' '.join(x) if type(x)==list else x).unstack(1).reset_index(level=0)
                                                Text      ...          NextWords
0  there are many different crayons in the bright...      ...             in the
1  there are many different crayons in the bright...      ...             of all
0  i like a lot of sports cars because they go re...      ...       because they
1  i like a lot of sports cars because they go re...      ...            to ride
0                                 all camels are fun      ...            are fun
[5 rows x 4 columns]

Post a Comment for "Unnest Grab Keywords/nextwords/beforewords Function"