在pandas df中查找列组合的计数

编程入门 行业动态 更新时间:2024-10-12 05:50:00
本文介绍了在pandas df中查找列组合的计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在 Pandas df 中有一张桌子

i have a table in pandas df

product_id_x product_id_y 1 2 1 3 1 4 3 7 3 11 3 14 3 2 and so on around (1000 rows)

我想找到每个 product_id_x 和 product_id_y 的组合数.

i want to find the count of combinations for each product_id_x with product_id_y.

即.1 与 1-2,1-3,1-4 组合(共 3 个组合)同样3有4种组合.

ie. 1 has combinations with 1-2,1-3,1-4(total 3 combinations) similarly 3 has total 4 combinations.

并创建一个具有

product_id_x combinations 1 3 3 4 and so on ..(distinct product_id_x's)

我应该遵循什么方法?我的 Python 技能处于初级水平.提前致谢.

what approach should i follow? my skills on python are at a beginner level. Thanks in advance.

推荐答案

size 计算每个列值对一起出现的行数.count 计数相同的东西,但它们不为空.由于您没有提到任何有关空值的内容,我将在 groupby 之后使用 size,然后使用 unstack

size counts the number of rows each of the column value pairs happen together. count counts the same thing but where they are not null. Since you did not mention anything about nulls, I'll use size after a groupby, then unstack

df.groupby(['product_id_x', 'product_id_y']).size().unstack(fill_value=0)

更多推荐

在pandas df中查找列组合的计数

本文发布于:2023-11-22 02:01:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615542.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   pandas   df

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!