python - 计算 pandas 数据框中的匹配组合

标签 python pandas combinations

我需要为以下问题找到更有效的解决方案:

给定的是一个数据框,每行有 4 个变量。我需要找到 8 个元素的列表,其中包含最大行数中每行的所有变量。

一个有效但非常慢的解决方案是创建第二个数据帧,其中包含所有可能的组合(基本上是没有重复的排列)。然后循环遍历每个组合并将其与初始数据帧进行比较。计算解决方案的数量并将其添加到第二个数据帧中。

import numpy as np
import pandas as pd
from itertools import combinations


df = pd.DataFrame(np.random.randint(0,20,size=(100, 4)), columns=list('ABCD'))
df = 'x' + df.astype(str)
listofvalues = df['A'].tolist()
listofvalues.extend(df['B'].tolist())
listofvalues.extend(df['C'].tolist())
listofvalues.extend(df['D'].tolist())
listofvalues = list(dict.fromkeys(listofvalues))
possiblecombinations = list(combinations(listofvalues, 6))
dfcombi = pd.DataFrame(possiblecombinations, columns = ['M','N','O','P','Q','R'])
dfcombi['List'] = dfcombi.M.map(str) + ',' + dfcombi.N.map(str) + ',' + dfcombi.O.map(str) + ',' + dfcombi.P.map(str) + ',' + dfcombi.Q.map(str) + ',' + dfcombi.R.map(str)
dfcombi['Count'] = ''
for x, row in dfcombi.iterrows():
        comparelist =  row['List'].split(',')
        pointercounter = df.index[(df['A'].isin(comparelist) == True) & (df['B'].isin(comparelist) == True) & (df['C'].isin(comparelist) == True) & (df['D'].isin(comparelist) == True)].tolist()
        row['Count'] = len(pointercounter)

我认为必须有一种方法可以避免 for - 循环并用一些指针替换它,我只是不知道如何实现。

谢谢!

最佳答案

您的代码可以重写为:

# working with integers are much better than strings
enums, codes = df.stack().factorize()

# encodings of df
s = [set(x) for x in enums.reshape(-1,4)]

# possible combinations
from itertools import combinations, product
possiblecombinations = np.array([set(x) for x in combinations(range(len(codes)), 6)])

# count the combination with issubset
ret = [0]*len(possiblecombinations)
for a, (i,b) in product(s, enumerate(possiblecombinations)):
    ret[i] += a.issubset(b)

# the combination with maximum count
max_combination = possiblecombinations[np.argmax(ret)]
# in code {0, 3, 4, 5, 17, 18}

# and in values: 
codes[list(max_combination)]
# Index(['x5', 'x15', 'x12', 'x8', 'x0', 'x6'], dtype='object')

所有这些花费了大约 2 秒,而您的代码花费了大约 1.5 分钟。

关于python - 计算 pandas 数据框中的匹配组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59288723/

相关文章:

python - 计算连通图

python - 无法从网站下载 pdf 文件

python - 如何分割大型 XBRL 文件?

python-3.x - 如何在 Pandas 中找到完整的空行

python - 在 sklearn.cross_validation.cross_val_score 中使用 python pandas 时间戳

r - 给定一个向量,返回大小为 n 的所有组合的列表

python memcache搜索字符串

Python:使用另一个文件作为键从文件中提取行

pandas - 有没有办法修复或绕过数据帧中特定列中奇怪的时间格式?

matlab - matlab中矩阵的所有组合