【问题标题】:Pivot Pandas Python Dataframe [duplicate]Pivot Pandas Python数据框[重复]
【发布时间】:2021-10-15 17:29:12
【问题描述】:

我正在处理这个数据集:

# dummy data
import pandas as pd
data = pd.DataFrame({None : ['Company', 'AAA', 'BBB', 'CCC','Company', 'AAA' ],
                     None : ['Copper', 'Copper', 'Iron', 'Iron', 'Gold', 'Gold'],
                     "NaN" : ['Net','Gross', 'Net','Gross', 'Net','Gross' ], 
                     "11/01/2021" : [1,2,3,4,5,6],
                     "12/01/2021" : [10,11,12,13,14,15],
                     "13/01/2021" : [19,20,21,22,23,25]})
data

我正在尝试得到这个输出:

我尝试使用 stack/melt,但无法获取这种格式的数据。

谢谢

【问题讨论】:

  • data.melt(data.columns[:2]) 之后重命名您的列

标签: python pandas dataframe stack pandas-melt


【解决方案1】:

您可以使用.melt() 转换表格,然后使用.rename().sort_values().reset_index() 格式化为所需的列名和行顺序:

(data.rename({None: 'Material', 'NaN': 'Revenue'}, axis=1)
     .melt(id_vars=['Material', 'Revenue'], var_name='Month', value_name='Value')
     .sort_values(['Material', 'Revenue'], ascending=[True, False])
).reset_index(drop=True)

结果:

   Material Revenue       Month  Value
0    Copper     Net  11/01/2021      1
1    Copper     Net  12/01/2021     10
2    Copper     Net  13/01/2021     19
3    Copper   Gross  11/01/2021      2
4    Copper   Gross  12/01/2021     11
5    Copper   Gross  13/01/2021     20
6      Gold     Net  11/01/2021      5
7      Gold     Net  12/01/2021     14
8      Gold     Net  13/01/2021     23
9      Gold   Gross  11/01/2021      6
10     Gold   Gross  12/01/2021     15
11     Gold   Gross  13/01/2021     25
12     Iron     Net  11/01/2021      3
13     Iron     Net  12/01/2021     12
14     Iron     Net  13/01/2021     21
15     Iron   Gross  11/01/2021      4
16     Iron   Gross  12/01/2021     13
17     Iron   Gross  13/01/2021     22

【讨论】:

    猜你喜欢
    • 2022-11-15
    • 1970-01-01
    • 1970-01-01
    • 2015-04-22
    • 1970-01-01
    • 2018-11-01
    • 2018-10-11
    • 2020-04-19
    • 1970-01-01
    相关资源
    最近更新 更多