簡體   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