简体   繁体   English

如何使用多索引将 pandas dataframe 中的单行与多行相加?

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

My dataframe with Quarter and Week as MultiIndex:我的 dataframe 与季度和周作为 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).我正在尝试将 Q1(Q1-W04)中的最后一行添加到 Q2(Q2-W15 到 Q2-W18)中的所有行中。 This is what I would like the dataframe to look like:这就是我希望 dataframe 的样子:

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.当我尝试仅指定 0 级索引并对特定行求和时,所有 Q2 值 go 为 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.我发现如果我同时指定0级和1级索引,就没有问题。

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?有没有办法将特定行与 Q2 0 级索引中的所有行相加,而不必通过其 1 级索引单独调用每一行?

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. df.loc 返回一个 DataFrame,设置它查找列表或数组的值。 Hence the above.因此以上。

In your case we should remove the impact of index在您的情况下,我们应该消除index的影响

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用行字符串的子集在 pandas dataframe 分组中将多行组合成单行 - How to combine multiple rows into a single row in pandas dataframe grouping using a subset of the row string 使用 Pandas MultiIndex 选择多行分层 DataFrame - Selecting multiple rows of hierarchical DataFrame with Pandas MultiIndex "如何将 Pandas 数据帧的所有行与另一个 Pandas 数据帧中的单行相乘?" - How to multiple all rows of a Pandas dataframe by a single row in another Pandas dataframe? Pandas:通过删除多索引数据框中的 NaN 将多行折叠成一行 - Pandas: Collapse many rows into a single row by removing NaN's in a multiindex dataframe 如何将多索引行附加到空的熊猫数据帧 - How to append MultiIndex rows to empty pandas dataframe 如何计算 pandas 中多索引 dataframe 的行中的斜率? - How to calculate slope in rows of a multiindex dataframe in pandas? 如何访问熊猫多索引数据框中的特定行 - How to access specific rows in a pandas multiindex dataframe Pandas:如何在多索引数据框中追加一行? - Pandas: How to append a row in multiindex dataframe? 如何使用 MultiIndex append 将“总计”行添加到 pandas dataframe - How to append a "Total" row to pandas dataframe with MultiIndex 如何使用 pandas 将多行转换为同一 ID 的单行 - how to convert multiple rows into single row for same id using pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM