简体   繁体   中英

How to sum single row to multiple rows in pandas dataframe using multiindex?

My dataframe with Quarter and Week as MultiIndex:

Quarter   Week      X   Y   Z
Q1        Q1-W01    1   1   1
          Q1-W02    2   2   2
          Q1-W03    3   3   3
          Q1-W04    4   4   4
Q2        Q2-W15    15  15  15
          Q2-W16    16  16  16
          Q2-W17    17  17  17
          Q2-W18    18  18  18

I am trying to add the last row in Q1 (Q1-W04) to all the rows in Q2 (Q2-W15 through Q2-W18). This is what I would like the dataframe to look like:

Quarter   Week      X   Y   Z
Q1        Q1-W01    1   1   1
          Q1-W02    2   2   2
          Q1-W03    3   3   3
          Q1-W04    4   4   4
Q2        Q2-W15    19  19  19
          Q2-W16    20  20  20
          Q2-W17    21  21  21
          Q2-W18    22  22  22

When I try to only specify the level 0 index and sumthe specific row, all Q2 values go to NaN.

df.loc['Q2'] += df.loc['Q1','Q1-W04'] 

Quarter   Week      X   Y   Z
Q1        Q1-W01    1   1   1
          Q1-W02    2   2   2
          Q1-W03    3   3   3
          Q1-W04    4   4   4
Q2        Q2-W15    NaN NaN NaN
          Q2-W16    NaN NaN NaN
          Q2-W17    NaN NaN NaN
          Q2-W18    NaN NaN NaN

I have figured out that if I specify both the level 0 and level 1 index, there is no problem.

df.loc['Q2','Q2-W15'] += df.loc['Q1','Q1-W04']

Quarter   Week      X   Y   Z
Q1        Q1-W01    1   1   1
          Q1-W02    2   2   2
          Q1-W03    3   3   3
          Q1-W04    4   4   4
Q2        Q2-W15    19  19  19
          Q2-W16    16  16  16
          Q2-W17    17  17  17
          Q2-W18    18  18  18

Is there a way to sum the specific row to all the rows within the Q2 Level 0 index without having to call out each row individually by its level 1 index?

Any insight/guidance would be greatly appreciated.

Thank you.

try this

df.loc['Q2'] = (df.loc['Q2'] + df.loc['Q1', 'Q1-W04']).values.tolist()

df.loc returns a DataFrame, to set the value it looks for the list or array. Hence the above.

In your case we should remove the impact of index

df.loc['Q2','Q2-W15'] += df.loc['Q1','Q1-W04'].values

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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