枢轴数据框并获取两个值列

发布于 2025-02-13 17:16:28 字数 2086 浏览 4 评论 0原文

我有一个数据集,看起来像这样:

地区国家产品组类别产品productfamilyreality其他信息
dk欧洲xyz1/2x
de欧洲x1 y1z1z1 z11/12-US
AmericasXYZ6/9-

想拥有一个像这样的枢轴

                                             Region
                                             Europe                                  Americas
                                             DK                   DE                 US
                                             Value Other info     Value Other info   Value Other info
ProductGroup    Category    ProductFamily
C               Y           Z                1/2  X                                  6/9  -
X1              Y1          Z1                                     1/12  -

我已经尝试过

x = df_out.pivot(index=['MATKL','ProductGroup','Category','ProductFamily'], 
                 columns=['Region','Country'], values = ['value','Eother info'])

它几乎可以完成工作,但是它为我带来了两个块,然后所有地区,国家,然后是其他信息,以获取其他信息。

I have a dataset there look like this:

RegionCountryProductGroupCategoryProductFamilyvalueother info
DKEuropeXyZ1/2X
DEEuropeX1y1Z11/12-
USAmericasXyZ6/9-

I Would like to have a pivot like this

                                             Region
                                             Europe                                  Americas
                                             DK                   DE                 US
                                             Value Other info     Value Other info   Value Other info
ProductGroup    Category    ProductFamily
C               Y           Z                1/2  X                                  6/9  -
X1              Y1          Z1                                     1/12  -

I have tried this

x = df_out.pivot(index=['MATKL','ProductGroup','Category','ProductFamily'], 
                 columns=['Region','Country'], values = ['value','Eother info'])

It does nearly the job, but it give me two blocks on for value and then all region, countries and then the same for Other info.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

金橙橙 2025-02-20 17:16:28

使用 > 带有 a>:

x = (df_out.pivot(index=['ProductGroup','Category','ProductFamily'], 
                 columns=['Region','Country'], values = ['value','other info'])
     .reorder_levels([1,2,0], axis=1)
     .sort_index(axis=1, level=[0,1], sort_remaining=False))
print (x)
Region                                  DE                DK             \
Country                             Europe            Europe              
                                     value other info  value other info   
ProductGroup Category ProductFamily                                       
X            y        Z                NaN        NaN    1/2          X   
X1           y1       Z1              1/12          -    NaN        NaN   

Region                                    US             
Country                             Americas             
                                       value other info  
ProductGroup Category ProductFamily                      
X            y        Z                  6/9          -  
X1           y1       Z1                 NaN        NaN  

Use DataFrame.reorder_levels with DataFrame.sort_index:

x = (df_out.pivot(index=['ProductGroup','Category','ProductFamily'], 
                 columns=['Region','Country'], values = ['value','other info'])
     .reorder_levels([1,2,0], axis=1)
     .sort_index(axis=1, level=[0,1], sort_remaining=False))
print (x)
Region                                  DE                DK             \
Country                             Europe            Europe              
                                     value other info  value other info   
ProductGroup Category ProductFamily                                       
X            y        Z                NaN        NaN    1/2          X   
X1           y1       Z1              1/12          -    NaN        NaN   

Region                                    US             
Country                             Americas             
                                       value other info  
ProductGroup Category ProductFamily                      
X            y        Z                  6/9          -  
X1           y1       Z1                 NaN        NaN  
谎言 2025-02-20 17:16:28

尝试以下操作:

df.set_index([*df][:-2]).unstack([0,1]).swaplevel(0,-1, axis=1).sort_index(level=0, axis=1)

Try this:

df.set_index([*df][:-2]).unstack([0,1]).swaplevel(0,-1, axis=1).sort_index(level=0, axis=1)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文