我在修复以下问题时遇到问题: 我有一个数据框,每一行上都有标记文本,看起来(有些东西)如下
index feelings
1 [happy, happy, sad]
2 [neutral, sad, mad]
3 [neutral, neutral, happy]
和单词列表lst1=[happy, fantastic]
,lst2=[mad, sad]
,lst3=[neutral]
,我想检查我的数据帧中每一行的单词在列表中出现的次数。因此,输出将如下所示:
index feelings occlst1 occlst2 occlst3
1 [happy, happy, sad] 2 1 0
2 [neutral, sad, mad] 0 2 1
3 [neutral, neutral, happy] 1 0 2
因此,我想创建一个新列,并将dataframe单元格与列表进行比较
提前谢谢
您可以构建一个参考系列,将感受与列表id相匹配。然后
explode
+merge
+pivot_table
:NB.我在这里考虑到
index
是一列,如果是索引,则需要添加df.reset_index()
步骤输出:
输入:
使用
collections.Counter
设置:
更新:根据@mozway的建议
注意:为了可读性,我没有使用除
feelings
之外的任何其他列。但是concat
函数从df
还原所有列输出:
您还可以使用:
输出:
相关问题 更多 >
编程相关推荐