[英]Python: Pandas: get all the combination and their frequency of different column
我有一个Python数据框有三列。
a b c
0 1 2 3
1 1 2 3
2 1 2 8
3 1 5 9
4 1 3 7
5 1 3 4
我想找到a,b,c的所有组合,我的预期结果是:
[1,2,3]: 2 # from row 0 and row 1
[1,2] : 3 # from row 0 and row 1 and row 2
[1,3] : 4 # from row 0, 1, 4, 5
[1,4] : 1
[1,5] : 1
[1,7] : 1
[1,8] : 1
[1,9] : 1
[2,3] : 2
............
随意使用任何包装。
import pandas as pd
pd.DataFrame(data=[[1,2,3],[1,2,3],[1,2,8],[1,5,9],[1,3,7],[1,3,4]],columns=['a','b','c'])
令人讨厌的单线:
In [114]: collections.Counter(map(str, itertools.chain.from_iterable(list(df.apply(lambda x: list(itertools.chain.from_iterable([list(itertools.combinations(x, k)) for k in range(1, 4)])), axis=1).values))))
Out[114]:
Counter({'(1, 2)': 3,
'(1, 2, 3)': 2,
'(1, 2, 8)': 1,
'(1, 3)': 4,
'(1, 3, 4)': 1,
'(1, 3, 7)': 1,
'(1, 4)': 1,
'(1, 5)': 1,
'(1, 5, 9)': 1,
'(1, 7)': 1,
'(1, 8)': 1,
'(1, 9)': 1,
'(1,)': 6,
'(2, 3)': 2,
'(2, 8)': 1,
'(2,)': 3,
'(3, 4)': 1,
'(3, 7)': 1,
'(3,)': 4,
'(4,)': 1,
'(5, 9)': 1,
'(5,)': 1,
'(7,)': 1,
'(8,)': 1,
'(9,)': 1})
一些解释:
首先,由于df.apply(..., axis=1
在每行上应用了lambda
函数。
lambda
函数将创建行值的所有可能组合,而不管条目的数量如何。
我们将所有找到的值合并到每行一个列表中。 那是第一个itertools.chain.from_iterable
插入的地方。
我们将所有行值合并到一个列表中,使用第二个itertools.chain.from_iterable
。
我们通过collections.Counter
来描述结果,并获得频率。
编辑
相同的解决方案,但不使用itertools.chain.from_iterable
In [25]: collections.Counter([str(k) for l in df.apply(lambda x: [c for i in range(1, 4) for c in itertools.combinations(x, i)], axis=1).values for k in l])
这次,我利用列表理解来达到相同的结果,这可能会导致解决方案更具可读性。 步骤大致相同,没有“列表合并”的麻烦。
from cytoolz import concat, mapcat
from functools import partial
from itertools import combinations
c = lambda x, k: combinations(x, k)
pd.value_counts(list(concat(concat(map(
partial(c, x),
range(2, df.shape[1] + 1)
)) for x in df.values.tolist())))
(1, 3) 4
(1, 2) 3
(1, 2, 3) 2
(2, 3) 2
(5, 9) 1
(1, 2, 8) 1
(1, 3, 4) 1
(2, 8) 1
(1, 4) 1
(1, 3, 7) 1
(1, 5, 9) 1
(1, 8) 1
(1, 9) 1
(1, 7) 1
(3, 7) 1
(3, 4) 1
(1, 5) 1
dtype: int64
随着@ juanpa.arrivillaga的建议mapcat
pd.value_counts(list(concat(
(mapcat(partial(c, x), range(2, df.shape[1] + 1)) for x in df.values.tolist())
)))
(1, 3) 4
(1, 2) 3
(1, 2, 3) 2
(2, 3) 2
(5, 9) 1
(1, 2, 8) 1
(1, 3, 4) 1
(2, 8) 1
(1, 4) 1
(1, 3, 7) 1
(1, 5, 9) 1
(1, 8) 1
(1, 9) 1
(1, 7) 1
(3, 7) 1
(3, 4) 1
(1, 5) 1
dtype: int64
可能有一种有效的方法,可能是以下一种方法:
import pandas as pd
from itertools import combinations
from collections import Counter
df = pd.DataFrame(data=[[1,2,3],[1,2,3],[1,2,8],[1,5,9],[1,3,7],[1,3,4]],columns=['a','b','c'])
# Get columns combination
# https://stackoverflow.com/a/43348187/5916727
cc = list(combinations(df.columns, 2))
# Append to new list for combinations
tmp_list = []
for columns in cc:
tmp_list.append(list(zip(df[columns[0]], df[columns[1]])))
# https://stackoverflow.com/a/32786226/5916727
tmp_list.append(list(zip(df.a, df.b, df.c)))
# Flatten the list
# https://stackoverflow.com/a/952952/5916727
flat_list = [item for sublist in tmp_list for item in sublist]
print(['{0}:{1}'.format(list(item), count) for item, count in Counter(flat_list).items()])
结果:
['[1, 2]:3',
'[5, 9]:1',
'[1, 2, 8]:1',
'[1, 3]:4',
'[2, 8]:1',
'[1, 3, 4]:1',
'[1, 3, 7]:1',
'[1, 4]:1',
'[1, 2, 3]:2',
'[1, 5]:1',
'[1, 8]:1',
'[2, 3]:2',
'[1, 9]:1',
'[1, 7]:1',
'[3, 7]:1',
'[3, 4]:1',
'[1, 5, 9]:1']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.