1

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
3
  • 4
    lambda row: [" ".join(x) for x in row] maybe? Commented Oct 28, 2021 at 14:41
  • I feel dumb, that's exactly what it needed. Thanks! Commented Oct 28, 2021 at 14:44
  • You mean .apply(lambda row: " ".join(x for x in row)) note, that is a generator expression, but it is totally pointless, it doesn't do anything. So just .apply(" ".join) would work Commented Oct 28, 2021 at 14:45

2 Answers 2

1

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])
Sign up to request clarification or add additional context in comments.

Comments

0

This should work :

df.n_grams.apply(lambda row: [" ".join(x) for x in row]) 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.