简体   繁体   English

如何在 pandas 中对索引和 pivot 列进行分组

[英]How can I groupby index and pivot columns in pandas

I have this dataframe:我有这个 dataframe:

在此处输入图像描述

And I'd like to unstack (or melt? or pivot?) the dataframe so that for every Coupon in the index, the SMM_diff gets its own column.而且我想取消堆叠(或融化?或 pivot?)dataframe 以便对于索引中的每个CouponSMM_diff都有自己的列。

Something like:就像是:

                 SMM_diff_2.0    SMM_diff_2.5  ... rest of coupons
y_m
2020-05-01      -0.010935       -0.011007             ...
2019-02-01       0.007838       -0.007824
2021-04-01       0.029721       -0.004892
2020-07-01      -0.011197      -0.018712
2022-01-01       0.14557        -0.001675             ...
       ...              ...            
                         

I'm not really sure how to approach this problem to be honest, so any help would be appreciated.老实说,我不确定如何解决这个问题,因此我们将不胜感激。 Thanks!谢谢!

Wow, so simple: df['SMM_diff'].unstack(level = 1)哇,这么简单: df['SMM_diff'].unstack(level = 1)

Does the trick.行得通。

在此处输入图像描述

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

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