旋转/旋转数据集的最佳方法 [英] Best way to Pivot/Rotate Data Set

查看:147
本文介绍了旋转/旋转数据集的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据框

df = pd.DataFrame({ '1': ['Mon (07/08)','Sales', '2'],
                     '2': ['Mon (07/08)','Stock','3'],
                    '3': ['Mon (07/08)','Qty','4'],
                  '4': ['Tue (08/08)','Sales', '4'],
                     '5': ['Tue (08/08)','Stock','5'],
                    '6': ['Tue (08/08)','Qty','6']})

df

我正在尝试获取以下输出,但是如果您能提出建议以获取以下输出,我将不胜感激

I'm trying to get the following output though would appreciate if you can suggest anything I should look at to get the following output

推荐答案

进行转置,然后使用格式化轴即可. rename_axis .

Take the transpose, then use pivot to reshape. After that, it's just a matter of formatting the axes with rename_axis.

# reshape
df = df.T.pivot(index=0, columns=1, values=2)

# format axes
df = df.rename_axis(None).rename_axis(None, 1)

结果输出:

            Qty Sales Stock
Mon (07/08)   4     2     3
Tue (08/08)   6     4     5

这篇关于旋转/旋转数据集的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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