熊猫从上一行获取差异比率,并将其值存储在另一列中,并具有多索引

问题描述

我想知道如何获取具有多索引列的两行之间的差异比率,并将其存储在特定的列中。

我有一个看起来像这样的数据框。

>>>df

                   A             B            C
                   total  diff   total  diff  total  diff 
  2020-08-15       100    0      200    0     20     0

每天,我都会添加一行。新行如下所示。

df_new

                   A             B            C
                   total  diff   total  diff  total  diff 
  2020-08-16       200     -      50    -     30     -

对于diff列,我想从上一行获取total的比率。因此公式将为([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

我知道如何添加新行。

day = dt.today()
df.loc[day.strftime("%Y-%m-%d"),:] = df_new.squeeze()

但是我不知道如何获得带有多索引列的两行之间的区别……任何帮助将不胜感激!谢谢。

解决方法

使用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
,

您可以使用df.xspd.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

如果您不想创建新的DataFrame(x),请使用DataFrame.append附加值。

在步骤idx = pd.IndexSlice之前,一切都是相同的,不要创建x,而是将值附加到df

df2.loc[:,'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