2
私は、次のパンダデータフレームDFを持っている:MultiIndexの条件でPandas Column条件付きで合計しますか?
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が痛々しいほど遅いです)、私は、あまりにもわからない私は和の結果を効率的に並べ替えることができる方法についての(新しく作成された)「Result」列に入力しますか?
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
内
cumsum
を使用してみてください。パンダでは、列とインデックスの名前を選択できます。 –df.assign(結果= df.groupby(レベル= '時間')。cumsum())が機能します。ありがとう。 – Bython