在lambda函数中使用列表推导?



我有一个包含n元组列表的数据框架。我想把元组列表转换成字符串列表。要做到这一点,我试图在lambda函数中使用列表理解。然而,我总是得到一个错误,说我的列表没有定义。

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)]

我正在尝试得到:

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 try:

data['n_grams'] = data.n_grams.apply(lambda row: " ".join(x) for x in row)

但是我一直得到错误:

NameError: name 'row' is not defined

感谢rdas的注释,解决方案只是使用硬括号:

data['n_grams'] = data.n_grams.apply(lambda row: [" ".join(x) for x in row])

应该可以:

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

相关内容

  • 没有找到相关文章

最新更新