【问题标题】:Pivot/ Unstack a Pandas Dataframe in Python在 Python 中旋转/取消堆叠 Pandas 数据框
【发布时间】:2018-08-22 13:54:17
【问题描述】:

我有以下数据框

                            01/01/2017             02/01/2017
 Productid   ProductName    Sales     Discount     Sales     Discount
 1           abc            100       12           234       23
 2           xyz            156       13           237       13
 3           pqr            300       12           198       18

我需要将其转换为以下数据框。

 Productid   ProductName    Date          Sales      Discount
 1           abc            01/01/2017    100        12
 1           abc            02/01/2017    234        23
 2           xyz            01/01/2017    156        13
 2           xyz            02/01/2017    237        13
 3           pqr            01/01/2017    300        12
 3           pqr            02/01/2017    198        18

如何在 Python 中做到这一点?

【问题讨论】:

  • 请提供文本数据框或重现它的代码。它比图像更容易响应。
  • 我不知道具体细节,但this question-answer 似乎是一个广泛的总结。
  • 我删除了图片。
  • @Vijay 我添加了一个可能的解决方案。请检查它是否适合您。
  • 成功了 :) 谢谢

标签: python pandas pivot


【解决方案1】:

多索引很难直接重现。所以首先根据 OP 的原始数据帧初始化数据帧。

df = pd.read_clipboard() #reading part of OP's Dataframe
df
    Productid   ProductName Sales   Discount    Sales.1 Discount.1
0           1           abc   100         12        234         23
1           2           xyz   156         13        237         13
2           3           pqr   300         12        198         18

df.columns = ['Productid', 'ProductName', 'Sales', 'Discount', 'Sales', 'Discount']
df.set_index(keys=['Productid','ProductName'],inplace=True)
df
                         Sales  Discount    Sales   Discount
Productid   ProductName             
        1           abc    100        12      234         23
        2           xyz    156        13      237         13
        3           pqr    300        12      198         18

array = [['01/01/2017','01/01/2017','02/01/2017','02/01/2017'],
         ['Sales', 'Discount', 'Sales',  'Discount']]
df.columns = pd.MultiIndex.from_arrays(array) #setting multi-index

假设这是 OP 的数据框:

df
                         01/01/2017         02/01/2017
                         Sales  Discount    Sales   Discount
Productid   ProductName             
        1           abc    100        12      234         23
        2           xyz    156        13      237         13
        3           pqr    300        12      198         18

使用stacklevel=0 参数的解决方案,然后在level=[0,1]reset_index() 上再次使用reset_index()。最后使用renameindex 列的名称更改为Date

df = df.stack(level=0).reset_index(level=[0,1]).reset_index()
df.rename(columns={'index':'Date'},inplace=True)
df[['Productid', 'ProductName','Date','Sales','Discount']]

    Productid   ProductName       Date  Sales   Discount
0           1           abc 01/01/2017    100         12
1           1           abc 02/01/2017    234         23
2           2           xyz 01/01/2017    156         13
3           2           xyz 02/01/2017    237         13
4           3           pqr 01/01/2017    300         12
5           3           pqr 02/01/2017    198         18

【讨论】:

    猜你喜欢
    • 2023-01-11
    • 2020-08-04
    • 2022-12-04
    • 2020-11-25
    • 2021-07-22
    • 2022-01-03
    • 1970-01-01
    • 2016-01-08
    • 2021-04-10
    相关资源
    最近更新 更多