首页 > 解决方案 > 连接熊猫数据报中的列表列表

问题描述

我有一个像这样的熊猫数据报。我想连接熊猫单元格中的嵌套列表。我在从 Python 中的列表列表中制作一个平面列表中提到了这个问题,它有所有可能的解决方案来做到这一点。我选择用 numpy 来做。但是,我收到此错误 - lambda row: [np.concatenate(x) for x in row]) ValueError: need at least one array to concatenate。我没有足够的python知识来自己解决这个问题。我应该如何修改我的方法以正确连接这些嵌套列表?

数据报

[[(ive, searching), (searching, right), (right, word), (word, thank), (thank, breather)], [(i, promise), (promise, wont), (wont, take), (take, help), (help, granted), (granted, fulfil), (fulfil, promise)], [(you, wonderful), (wonderful, blessing), (blessing, time)]]                                                            

[[(free, entry), (entry, 2), (2, wkly), (wkly, comp), (comp, win), (win, fa), (fa, cup), (cup, final), (final, tkts), (tkts, 21st), (21st, may), (may, 2005)], [(text, fa), (fa, 87121), (87121, receive), (receive, entry), (entry, questionstd), (questionstd, txt), (txt, ratetcs), (ratetcs, apply), (apply, 08452810075over18s)]]

[[(nah, dont), (dont, think), (think, go), (go, usf), (usf, life), (life, around), (around, though)]]                                                                                                                                                                                                                                 

[[(even, brother), (brother, like), (like, speak), (speak, me)], [(they, treat), (treat, like), (like, aid), (aid, patent)]] 

连接方法

def toFlatListBigram(fullCorpus):
flatListBigram = fullCorpus['bigrams'].apply(
    lambda row: [np.concatenate(x) for x in row])
return flatListBigram

标签: pythonpandasnlp

解决方案


这有效

def toFlatListBigram(fullCorpus):
flatListBigram = fullCorpus['bigrams'].apply(
    lambda row: [item for sublist in row for item in sublist])
return flatListBigram

推荐阅读