计算多索引数据帧中值出现次数的最快方法

2024-04-25 09:48:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个多索引dataframe,有许多级别和列。我正在寻找一种最快速的方法来迭代数据帧并计算,对于每一行,每个数据帧中有多少个单元格高于某个特定值,然后找到两个数据帧中至少有一个计数的行的交集。你知道吗

现在我正在使用for循环和groupby组合遍历数据帧,但是要找到正确的答案(我真正的数据帧由数千个级别和数百个列组成)花费了我太多的时间,所以我需要找到一种不同的方法。你知道吗

例如:

idx = pd.MultiIndex.from_product([[0,1],[0,1,2]],names= 
['index_1','index_2'])
 col = ['column_1', 'column_2']


values_list_a=[[1,2],[2,2],[2,1],[-8,1],[2,0],[2,1]]
DFA = pd.DataFrame(values_list_a, idx, col)

DFA:
                   columns_1 columns2
index_1 index_2
  0       0            1        2
          1            2        2
          2            2        1
  1       0            -8       1
          1            2        0
          2            2        1

values_list_b=[[2,2],[0,1],[2,2],[2,2],[1,0],[1,2]]
DFB = pd.DataFrame(values_list_b, idx, col)

DFB:
                   columns_1 columns2
index_1 index_2
  0       0            2        2
          1            0        1
          2            2        2
  1       0            2        2
          1            1        0
          2            1        2

我期望的是:

步骤1计数发生:

DFA:
                   columns_1 columns2 counts
index_1 index_2
  0       0            1        2       1
          1            2        2       2
          2            2        1       1
  1       0            -8       1       0
          1            2        0       1
          2            2        1       1

DFB:
                   columns_1 columns2 counts
index_1 index_2
  0       0            2        2        2
          1            0        1        0
          2            2        2        2
  1       0            2        2        2
          1            1        0        0
          2            1        2        1

第2步:计数大于0的2个数据帧的交集应创建一个新的数据帧,如下所示(记录在同一索引中至少有一个计数的两个数据帧的行,并添加一个新的索引级别)。索引\u 0=0应指DFA,索引\u 0=1应指DFB:

DFC:
                            columns_1 columns2 counts
  index_0 index_1 index_2
     0       0       0            1        2       1
                     2            2        1       1
             1       2            2        1       1

     1       0       0            2        2       2
                     2            2        2       2
             1       2            1        2       1

Tags: columns数据方法indexcol级别listpd
3条回答
df.groupby(['index_0','index_1', 'index2'])

现在,您需要使用与sql等价的

df.filter(lambda x: len(x.columns_1) > 2)
df.count()

这是个概念,我不明白你想过滤什么, 请注意,x是一个组,因此需要对其进行运算(len、set、values)等

pd.concat然后magic

def f(d, thresh=1):
    c = d.gt(thresh).sum(1)
    mask = c.gt(0).groupby(level=[1, 2]).transform('all')
    return d.assign(counts=c)[mask]

pd.concat({'bar': DFA, 'foo': DFB}, names=['index_0']).pipe(f)

                         column_1  column_2  counts
index_0 index_1 index_2                            
bar     0       0               1         2       1
                2               2         1       1
        1       2               2         1       1
foo     0       0               2         2       2
                2               2         2       2
        1       2               1         2       1

有意见

def f(d, thresh=1):
    # count how many are greater than a threshold `thresh` per row
    c = d.gt(thresh).sum(1)

    # find where `counts` are > `0` for both dataframes
    # conveniently dropped into one dataframe so we can do
    # this nifty `groupby` trick
    mask = c.gt(0).groupby(level=[1, 2]).transform('all')
    #                                    \   -/
    #                         This is key to broadcasting over 
    #                         original index rather than collapsing
    #                         over the index levels we grouped by

    #     create a new column named `counts`
    #         /      \ 
    return d.assign(counts=c)[mask]
    #                         \ /
    #                    filter with boolean mask

# Use concat to smash two dataframes together into one
pd.concat({'bar': DFA, 'foo': DFB}, names=['index_0']).pipe(f)

使用filter、.any()和pd.合并()

重新创建数据帧:

idx = pd.MultiIndex.from_product([[0,1],[0,1,2]], names=['one', 'two'])
columns = ['columns_1', 'columns_2']

DFA = pd.DataFrame(np.random.randint(-1,3, size=[6,2]), idx, columns)
DFB = pd.DataFrame(np.random.randint(-1,3, size=[6,2]), idx, columns)

print(DFA)

             columns_1  columns_2
one two                      
0   0           -1          2
    1            2         -1
    2           -1          0
1   0            1          2
    1            0          0
    2           -1         -1



print(DFB)

             columns_1  columns_2
one two                      
0   0            2         -1
    1            1          2
    2            2          1
1   0            0          0
    1           -1          2
    2            1         -1

在此实例中,筛选值大于1的数据帧。你知道吗

DFA = DFA.loc[(DFA>1).any(bool_only=True, axis=1),:]
DFB = DFB.loc[(DFB>1).any(bool_only=True, axis=1),:]

print(DFA)

             columns_1  columns_2
one two                      
0   0           -1          2
    1            2         -1
1   0            1          2

print(DFB)

        columns_1  columns_2
one two                      
0   0            2         -1
    1            1          2
    2            2          1
1   1           -1          2

将两者合并在一起。使用out join可以让你更接近。不确定是否要跳出索引,但是第一级0[0,1]是DFA。你知道吗

         columns_1_x  columns_2_x  columns_1_y  columns_2_y
one two                                                    
0   0           -1.0          2.0          2.0         -1.0
    1            2.0         -1.0          1.0          2.0
1   0            1.0          2.0          NaN          NaN
0   2            NaN          NaN          2.0          1.0
1   1            NaN          NaN         -1.0          2.0

相关问题 更多 >

    热门问题