![](/img/trans.png)
[英]Python - pandas - find most frequent combination with tie-resolution - performance
[英]Python: How to find most frequent combination of elements?
机器提供熊猫数据帧中提供的故障代码。 id
标识机器, code
为故障代码:
df = pd.DataFrame({
"id": [1,1,1,1,1,2,2,2,2,3,3,3,3,3,3,4],
"code": [1,2,5,8,9,2,3,5,6,1,2,3,4,5,6,7],
})
阅读示例:机器 1 生成了 5 个代码:1、2、5、8 和 9。
我想找出所有机器上最常见的代码组合。 该示例的结果将类似于[2]
(3x)、 [2,5]
(3x)、 [3,5]
(2x) 等。
我怎样才能做到这一点? 由于有大量数据,我正在寻找有效的解决方案。
以下是表示数据的另外两种方法(以防计算更容易):
pd.crosstab(df.id, df.code)
df.groupby("id")["code"].apply(list)
使用自定义函数all_subsets
,然后按Series.explode
压平值,最后使用Series.value_counts
:
from itertools import chain, combinations
#https://stackoverflow.com/a/5898031
#only converted to list and removed empty tuples by range(1,...
def all_subsets(ss):
return list(chain(*map(lambda x: combinations(ss, x), range(1, len(ss)+1))))
s = df.groupby('id')['code'].apply(all_subsets).explode().value_counts()
print (s)
(2,) 3
(2, 5) 3
(5,) 3
(1, 2) 2
(3, 6) 2
..
(1, 5, 8) 1
(9,) 1
(1, 3, 4, 6) 1
(5, 8, 9) 1
(4, 6) 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.