代码之家  ›  专栏  ›  技术社区  ›  rafvasq

给定一组索引,在dataframe中的每个索引之间聚合

  •  0
  • rafvasq  · 技术社区  · 5 年前

    给定一个索引数组,

    array([0, 12, 42, 50, 64, 67, 85, 90, 100]
    

    df df.iloc[i:i+1].sum() ?

    1 回复  |  直到 5 年前
        1
  •  3
  •   Scott Boston    5 年前

    IIUC,你可以用这个方法:

    df = pd.DataFrame(np.arange(0,100))
    a = np.array([0, 12, 42, 50, 64, 67, 85, 90, 100])
    
    df.groupby(pd.cut(df.index, a)).sum()
    

                  0
    (0, 12]      78
    (12, 42]    825
    (42, 50]    372
    (50, 64]    805
    (64, 67]    198
    (67, 85]   1377
    (85, 90]    440
    (90, 100]   855