开发者社区 问答 正文

如何将所有值与panda中的索引值相加?

我想计算type1和type2元素之间的一些值。例如,如果有像(a,b)和(b,a)这样的索引,那么它将等于(a,b)+(b,a)

问题来源StackOverflow 地址:/questions/59387305/how-can-i-sum-all-values-depend-on-index-value-in-pandas

展开
收起
kun坤 2019-12-25 21:37:48 372 分享 版权
1 条回答
写回答
取消 提交回答
  • 一种使用方法:

    df = df.reset_index()
    df['total'].groupby(df[['type1', 'type2']].apply(frozenset, 1)).sum()
    

    输出:

    (b, a)    15
    (c, a)    19
    Name: total, dtype: int64
    
    2019-12-25 21:37:54
    赞同 展开评论
问答分类:
问答地址: