I have a dataframe with a list of n-gram tuples. I want to convert the list of tuples to list of strings. To do this, I am trying to use list comprehension within my lambda function. However, I keep getting an error saying that my list isn't defined.
id n_grams
1 [(thanks), (thanks, past), (thanks, past, blue)]
2 [(support), (support, arm), (support, arm, brace), (support, arm, brace, left)]
3 [(blue), (blue, sky), (blue, sky, rain)]
4 [(breaking), (breaking, news), (breaking, news, fire), (breaking, news, fire, aparment)]
I am trying to get:
id n_grams
1 ["thanks", "thanks past", "thanks past blue"]
2 ["support", "support arm", "support arm brace", "support arm brace left"]
3 ["blue", "blue sky", "blue sky rain"]
4 ["breaking", "breaking news", "breaking news fire", "breaking news fire apartment"]
I have tried:
data['n_grams'] = data.n_grams.apply(lambda row: " ".join(x) for x in row)
But I keep getting the error:
NameError: name 'row' is not defined
CodePudding user response:
Thanks to the comment by rdas, the solution was simply using hard brackets:
data['n_grams'] = data.n_grams.apply(lambda row: [" ".join(x) for x in row])
CodePudding user response:
This should work :
df.n_grams.apply(lambda row: [" ".join(x) for x in row])