pandas :取消融化数据框以添加任意数量的列? [英] Pandas: unmelt dataframe to add arbitrary number of columns?

查看:143
本文介绍了 pandas :取消融化数据框以添加任意数量的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Pandas中有一个数据框df,如下所示:

I've got a dataframe df in Pandas that looks like this:

stores           product           discount
Westminster      102141            T
Westminster      102142            F
City of London   102141            T
City of London   102142            F
City of London   102143            T

最后,我想得到一个像这样的数据集:

And I'd like to end up with a dataset that looks like this:

stores           product_1  discount_1 product_2  discount_2 product_3  discount_3
Westminster      102141     T          102143     F       
City of London   102141     T          102143     F          102143     T

如何在熊猫中做到这一点?

How do I do this in pandas?

我认为这是stores列上的一种枢纽,但有多个.还是说它是未融化"而不是枢轴化"?

I think this is some kind of pivot on the stores column, but with multiple . Or perhaps it's an "unmelt" rather than a "pivot"?

我尝试过:

df.pivot("stores", ["product", "discount"], ["product", "discount"])

但是我得到了TypeError: MultiIndex.name must be a hashable type.

推荐答案

使用

Use DataFrame.unstack for reshape, only necessary create counter by GroupBy.cumcount, last change ordering of second level and flatten MultiIndex in columns by map:

df = (df.set_index(['stores', df.groupby('stores').cumcount().add(1)])
        .unstack()
        .sort_index(axis=1, level=1))
df.columns = df.columns.map('{0[0]}_{0[1]}'.format)
df = df.reset_index()
print (df)
           stores discount_1  product_1 discount_2  product_2 discount_3  \
0  City of London          T   102141.0          F   102142.0          T   
1     Westminster          T   102141.0          F   102142.0        NaN   

   product_3  
0   102143.0  
1        NaN  

这篇关于 pandas :取消融化数据框以添加任意数量的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆