python - 计算多索引 pandas 数据框中值出现的最快方法

标签 python python-3.x pandas dataframe count

我有两个具有多个级别和列的多索引数据框。我正在寻找迭代数据帧并计数的最快方法,对于每一行,每个数据帧中有多少个单元格高于特定值,然后找到两个数据帧中至少获得一个计数的行的交集。

现在我正在结合使用 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 个数据帧的交集应创建一个像这样的新数据帧(其中记录在同一索引中至少获得一个计数的两个数据帧的行,并添加新的 index_0 级别) 。 index_0 = 0 应该引用 DFA,index_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

最佳答案

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)

关于python - 计算多索引 pandas 数据框中值出现的最快方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56482811/

相关文章:

python - 为数据分析构建 Python 代码

python - pandas.scatter_matrix 函数开始绘制模糊和损坏的图

python - 将(例如)8 更改为 08...python

python - Tkinter pyimage 不存在

运行 os.system 时出现 Python 错误 ("kinit") - sh : 1: kinit: not found

python - 为什么 df.isnull().sum() 会这样工作?

python - 根据 pandas 数据框中的两个条件创建一列 bool 值

python - 在python中将字符串日期转换为日期格式?

python - 如何附加到python 3中的字节

python - 使用并发 future 时如何共享状态