gpt4 book ai didi

python - 我如何求和取决于 pandas 中索引值的所有值?

转载 作者:行者123 更新时间:2023-11-30 21:54:15 24 4
gpt4 key购买 nike

我想计算 type1 和 type2 元素之间的一些值。例如,如果索引如下: (a,b)(b,a) 那么它将等于 (a,b)+(b,a ) 如果存在反向索引,我想对值进行求和。

Table screenshot

最佳答案

使用frozenset的一种方法:

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

输出:

(b, a)    15
(c, a) 19
Name: total, dtype: int64

关于python - 我如何求和取决于 pandas 中索引值的所有值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59387305/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com