python - 连接熊猫数据报中的列表列表
问题描述
我有一个像这样的熊猫数据报。我想连接熊猫单元格中的嵌套列表。我在从 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
解决方案
这有效
def toFlatListBigram(fullCorpus):
flatListBigram = fullCorpus['bigrams'].apply(
lambda row: [item for sublist in row for item in sublist])
return flatListBigram
推荐阅读
- javascript - 移除渲染组件时的 Vue 内存泄漏
- sql - 将递增值与其他值一起添加到 Oracle 数据库中的列中
- kotlin - 从类中获取可为空的 var
- angularjs - UI Grid gridApi.selection.getSelectedRows() 没有按照选择的顺序给出行
- python-3.x - 使用CAD时如何用放码点对NURBS曲线进行放码?
- automationanywhere - 如何使用 Automation Anywhere 工具从一年中的当前日期查找每个季度的 25 日?
- python - 如何将散列密码与 Python 中的 Input() 进行比较?
- apache-camel - 如何在骆驼单义性固定解析器中允许更短的行?
- php - 如何配置 symfony 来记录弃用?
- python - 将字典列表导出到excel