python - 如何计算熊猫数据框中选定列中值的唯一组合,包括值为0的频率?
问题描述
在我的数据框中(假设它称为 df),我有两列:一列标记为颜色,一列标记为 TOY_ID。使用df.groupby(['Colour', 'TOY_ID']).size()
我能够生成第三列,它表示其他两列的值出现在我的 df 中的次数的频率。输出示例如下所示:
Colour TOY_ID
Blue 31490.0 50
31569.0 50
50360636.0 20
..
Yellow 50360636.0 25
50366678.0 9
..
Green 31490.0 17
50366678.0 10
尽管此方法有效,但它没有显示前两列值为 0 的组合。我知道这可以在 R 中完成,但我不确定如何在 Python 中做到这一点。我想要的输出示例如下。有什么建议么?
Colour TOY_ID
Blue 31490.0 50
31569.0 50
50360636.0 20
50366678.0 0
..
Yellow 31490.0 0
31569.0 0
50360636.0 25
50366678.0 9
..
Green 31490.0 17
31569.0 0
50360636.0 0
50366678.0 10
解决方案
Series.reindex
与 一起使用MultiIndex.from_product
:
s = df.groupby(['Colour', 'TOY_ID']).size()
s = s.reindex(pd.MultiIndex.from_product(s.index.levels), fill_value=0)
print (s)
Colour TOY_ID
Blue 31490.0 50
31569.0 50
50360636.0 20
50366678.0 0
Green 31490.0 17
31569.0 0
50360636.0 0
50366678.0 10
Yellow 31490.0 0
31569.0 0
50360636.0 25
50366678.0 9
Name: a, dtype: int64
推荐阅读
- mongodb - mongodb 聚合 addFields 查询(在 kotlin + 缝合中) - 多个文档一起不起作用
- arrays - 如何在 F# 中均匀分布范围内的数字
- wordpress - 创建变体不会将数据添加到 wp_term_relationships
- c# - 将 IList 对象转换为 List
- vb.net - 比较富文本框时如何提取整个 XML 标记?
- c# - 如何检查所有文本框是否已填充?
- r - R:在函数中使用聚合,不起作用
- sql-server - 如何获得日期和时间的差异以及日期的总和?
- c# - c# - 添加附件到 Discord Webhook 消息
- sql - 在 biq 查询中将字符串转换为表列