繁体   English   中英

根据列的组合查找值

[英]Find value based on a combination of columns

有没有办法根据列值的组合来查找值?

例子:

df = pd.DataFrame({
    'One' : [np.random.randint(1, 10) for i in range(10)],
    'Two' : [np.random.randint(1, 10) for i in range(10)],
    'Three' : [np.random.randint(1, 10) for i in range(10)],
    'Four' : [np.random.randint(1, 10) for i in range(10)],
})

.

In [6]: df
Out[6]:
   One  Two  Three  Four
0    8    1      7     5
1    6    3      3     3
2    4    7      5     2
3    4    2      6     9
4    1    7      1     9
5    9    8      3     8
6    4    8      4     4
7    1    9      7     1
8    4    2      6     4
9    1    3      7     7

我有以下 df 并且我很感兴趣我需要组合哪些列来获得数字 9。这将是一个基于列的组合,因此每一行将 output 组合起来找到 9 的列的名称。

  • 在示例中,第 0 行将给出结果: ['One', 'Two']

  • 第 1 行:将给出'One'和所有其他三个的组合。 ['One', 'Two'], ['One', 'Three'], ['One', 'Four']

  • 第 2 行:错误

  • 第 3 行:['四']

等等...

注意:DataFrame 应该保持不变。

感谢您对未来的任何建议或帮助。

首先获取 dataframe 的所有列名组合,您可以使用itertools.combinations ,然后创建一个 function 将计算每个列名组合的sum ,如果sum等于,则将此类组合存储在临时列表中所需的总和,最后返回列组合列表,并将此 function 应用于 dataframe,对于axis=1

import itertools
cols = [j for i in [[list(col)
                     for col in (itertools.combinations(df.columns, i))]
                    for i in range(1, df.shape[1] + 1)]
        for j in i]
def getSubArray(row, sum_=9):
    result=[]
    for col in cols:
        if row.loc[col].sum()==sum_:
            result.append(col)
    return result 
result = df.apply(getSubArray, axis=1)

OUTPUT:

0                                                   [[One, Two]]
1    [[One, Two], [One, Three], [One, Four], [Two, Three, Four]]
2                                    [[One, Three], [Two, Four]]
3                                                       [[Four]]
4                                    [[Four], [One, Two, Three]]
5                                                        [[One]]
6                                                             []
7                                    [[Two], [One, Three, Four]]
8                                                             []
9                                                             []
dtype: object

您可以将所需总和的任何值从.apply调用传递给getSubArray ,例如: df.apply(getSubArray, axis=1, sum_=24)

from itertools import combinations
def find_9(x, v=9):
    c = combinations(x.index, 2)
    columns = []
    for i,j in c:
        if (x[i]+x[j]) == v:
            columns.append([i,j])
    return columns if columns else False

df.apply(lambda x: find_9(x),axis= 1)

输出:

0                               [[One, Two]]
1    [[One, Two], [One, Three], [One, Four]]
2                [[One, Three], [Two, Four]]
3                                      False
4                                      False
5                                      False
6                                      False
7                                      False
8                                      False
9                                      False

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM