繁体   English   中英

Pandas 取上一行的差值并将值存储在另一列中,具有多索引

[英]Pandas taking ratio of difference from the row above and store the value in another column, with multi-index

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

我有一个看起来像这样的 dataframe。

>>>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.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

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

在步骤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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM