gpt4 book ai didi

python - 以 MultiIndex 值为条件沿 Pandas Column 求和?

转载 作者:行者123 更新时间:2023-11-28 22:27:09 25 4
gpt4 key购买 nike

我有以下 Pandas DataFrame df:

                                 Value
time Position
1493791210867023000 0.0 21156.0
1.0 1230225.0
2.0 1628088.0
3.0 2582359.0
4.0 3388164.0
1493791210880251000 0.0 21156.0
1.0 1230225.0
2.0 1628088.0
3.0 2582359.0
4.0 3388164.0
1493791210888418000 0.0 21156.0
1.0 1230225.0
... ... ...

我怎样才能有效地沿着索引“位置”求和?我要实现的确切求和公式是:

                                 Value        Result
time Position
1493791210867023000 0.0 21156.0 Sum from 0.0 to 0.0
1.0 1230225.0 Sum from 0.0 to 1.0
2.0 1628088.0 Sum from 0.0 to 2.0
3.0 2582359.0 Sum from 0.0 to 3.0
4.0 3388164.0 Sum from 0.0 to 4.0
1493791210880251000 0.0 21156.0 Sum from 0.0 to 0.0
1.0 1230225.0 Sum from 0.0 to 1.0
2.0 1628088.0 Sum from 0.0 to 2.0
3.0 2582359.0 Sum from 0.0 to 3.0
... ... ... ...

我当前的解决方案花费的时间太长(IndexSlice 非常慢)而且我不太确定如何将总和结果有效地排序到(新创建的)“结果”列中?

import pandas as pd
import numpy as np
idx = pd.IndexSlice
res = {}
for i in range(5):
res[i] = df.loc[idx[:, :i]].groupby(level="time").sum()
df["Result"] = 0 #fill Result now with res[i] depending on position

最佳答案

尝试在 groupby 中使用 cumsum

df.assign(Result=df.groupby(level='time').Value.cumsum())
# suggested by @ScottBoston for pandas 0.20.1+
# df.assign(Result=df.groupby('time').Value.cumsum())

Value Result
time Position
1493791210867023000 0.0 21156.0 21156.0
1.0 1230225.0 1251381.0
2.0 1628088.0 2879469.0
3.0 2582359.0 5461828.0
4.0 3388164.0 8849992.0
1493791210880251000 0.0 21156.0 21156.0
1.0 1230225.0 1251381.0
2.0 1628088.0 2879469.0
3.0 2582359.0 5461828.0
4.0 3388164.0 8849992.0
1493791210888418000 0.0 21156.0 21156.0
1.0 1230225.0 1251381.0

关于python - 以 MultiIndex 值为条件沿 Pandas Column 求和?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44186122/

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