简体   繁体   English

在 dataframe 中减去两组 Pandas Multiindex

[英]Subtract two groups of Pandas Multiindex in a dataframe

I have a multiindexed dataframe, for example:我有一个多索引 dataframe,例如:

    df = pd.DataFrame(np.random.randn(4,2), index=pd.MultiIndex.from_tuples([(1900, 'elem1'), (1900, 'elem2'), (1901, 'elem1'), (1901, 'elem2')]),
                      columns=['col1', 'col2'])
    df.index.names=['y', 'elem']
    
df

                col1      col2
y    elem                     
1900 elem1  0.590143 -0.050658
     elem2  0.208803  1.739487
1901 elem1 -2.336184  0.151083
     elem2 -0.217127 -0.511950

I am trying to get the difference between 1900 and 1901 as part of the dataframe, as shown below:我试图将 1900 和 1901 之间的差异作为 dataframe 的一部分,如下所示:

                col1      col2
y    elem                     
1900 elem1  0.590143 -0.050658
     elem2  0.208803  1.739487
1901 elem1 -2.336184  0.151083
     elem2 -0.217127 -0.511950
diff elem1 -2.926327  0.201741
     elem2 -0.42593  -2.251437

Any advice how I could archive this task?有什么建议我可以如何存档这个任务? Your help is much appreciated!非常感谢您的帮助!

Subtract 1900 from 1901, append the diff to the index and concatenate back to the main df:从 1901 中减去 1900,append diff到索引并连接回主 df:

temp = (df.loc[1901]
          .sub(df.loc[1900], axis = 0)
          .set_index([['diff', 'diff']], append = True)
          .swaplevel()
        )
pd.concat([df, temp])

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

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