python - pandas 在文档单词矩阵中转换文档单词列表
问题描述
我有一个这样的熊猫数据集:
Brand AssociatedWord Weight
0 pepsi red 10
1 pepsi yellow 3
2 coke red 5
3 coke grey 5
4 coke pink 2
我需要将其转换为以下矩阵:
Brand red yellow grey pink
0 pepsi 10 3 0 0
1 coke 5 0 5 2
现在每一行都是一个品牌,每个关联词都有一列,其中报告了关联的权重。零值表示缺少关联。列的顺序并不重要。你能帮助我吗?
解决方案
new_df=df.pivot_table(index='Brand',columns='AssociatedWord',values='Weight',fill_value=0).reset_index()
print(new_df)
AssociatedWord Brand grey pink red yellow
0 coke 5 2 5 0
1 pepsi 0 0 10 3
注意:AssociatedWord 是列的名称,您可以使用以下方法更改它:
new_df.columns.name=None
Brand grey pink red yellow
0 coke 5 2 5 0
1 pepsi 0 0 10 3
new_df=df.set_index(['Brand','AssociatedWord']).unstack(fill_value=0).reset_index()
print(new_df)
new_name Brand Weight
AssociatedWord grey pink red yellow
0 coke 5 2 5 0
1 pepsi 0 0 10 3
推荐阅读
- java - 如何在图像的不同部分连续放置水印
- excel - 无法更改单元格值
- python - 在 localhost 上执行 python 脚本的 plpythonu 函数不起作用
- python - 比较所有行的多个特定列
- python - flask-restful 中的 Catch-All URL 示例也捕获 /
- c - 从进程上下文计算 1 秒内的中断数
- angular - 排序primeng p-table
- android - Android:为什么 Espresso 不能在 compileSdkVersion 27 上运行?
- reactjs - 重新渲染后保持滚动
- php - 如何将字符串转换为对象数组