[英]Pandas taking ratio of difference from the row above and store the value in another column, with multi-index
I want to know how to get the difference ratio between two rows with multiindexed columns, and store them in specific columns.我想知道如何获取具有多索引列的两行之间的差异比率,并将它们存储在特定列中。
I have a dataframe which looks like this.我有一个看起来像这样的 dataframe。
>>>df
A B C
total diff total diff total diff
2020-08-15 100 0 200 0 20 0
Everyday, I add one new row.每天,我都会添加一个新行。 The new row looks like this.新行看起来像这样。
df_new
A B C
total diff total diff total diff
2020-08-16 200 - 50 - 30 -
And for the columns diff
, I want to take the ratio from the row above, for the value of total
.对于列diff
,我想从上面的行中获取比率,作为total
的值。 So the formula will be ([total of today] - [total of the day before]) / [total of the day before]
所以公式将是([total of today] - [total of the day before]) / [total of the day before]
A B C
total diff total diff total diff
2020-08-15 100 0 200 0 20 0
2020-08-16 200 1.0 50 -0.75 30 0.5
I know how to add a new row.我知道如何添加新行。
day = dt.today()
df.loc[day.strftime("%Y-%m-%d"), :] = df_new.squeeze()
But I don't know how I can get the difference between two rows with multiindexed columns... Any help would be appreciated.但我不知道如何获得具有多索引列的两行之间的差异......任何帮助将不胜感激。 Thank you.谢谢你。
Use shift
to calculate the results and update the original df:使用shift
计算结果并更新原始 df:
s = df.filter(like="total").rename(columns={"total":"diff"}, level=1)
res = ((s - s.shift(1))/s.shift(1))
df.update(res)
print (df)
A B C
total diff total diff total diff
2020-08-15 100 0.0 200 0.00 20 0.0
2020-08-16 200 1.0 50 -0.75 30 0.5
You can use df.xs
and use pd.IndexSlice
to update MultiIndexed values.您可以使用df.xs
并使用pd.IndexSlice
来更新 MultiIndexed 值。
#df
# A B C
# total diff total diff total diff
#0 100 0 200 0 20 0
#df2
# A B C
# total diff total diff total diff
#0 200.0 NaN 50.0 NaN 30.0 NaN
# Take last row of current DataFrame i.e. `df`
curr = df.iloc[-1].xs('total', level=1) #Get total values
# Take total values of new DataFrame you get everyday i.e. `df2`
new = df2.iloc[0].xs('total',level=1)
# Calculate diff values
diffs = new.sub(curr).div(curr) # This is equal to `(new-curr)/curr`
idx = pd.IndexSlice
x = pd.concat([df, df2]).reset_index(drop=True)
x.loc[x.index[-1], idx[:,'diff']] = diffs.tolist()
x
A B C
total diff total diff total diff
0 100.0 0.0 200.0 0.00 20.0 0.0
1 200.0 1.0 50.0 -0.75 30.0 0.5
If you dont want to create new DataFrame( x
), then use DataFrame.append
to append values.如果您不想创建新的 DataFrame( x
),则使用DataFrame.append
到 append 值。
Everything is the same use until the step idx = pd.IndexSlice
, don't create x
but append values to df
在步骤idx = pd.IndexSlice
之前,一切都是一样的,不要创建x
而是将 append 值创建为df
df2.loc[:, idx[:,'diff']] = diffs.tolist()
df.append(df2)
A B C
total diff total diff total diff
0 100.0 0.0 200.0 0.00 20.0 0.0
0 200.0 1.0 50.0 -0.75 30.0 0.5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.