Python Pandas 将选定的列转换为行
Python Pandas convert selective columns into rows
我的数据集包含一些关于不同年份的价格和销售额的信息。问题是每年的价格和销售额实际上都是不同的 header 列。例如 CSV 看起来像
Items
Price in 2018
Price in 2019
Price in 2020
Sales in 2018
Sales in 2019
Sales in 2020
A
100
120
135
5000
6000
6500
B
110
130
150
2000
4000
4500
C
150
110
175
1000
3000
3000
我想给它展示这样的东西
Items
Year
Price
Sales
A
2018
100
5000
A
2019
120
6000
A
2020
135
6500
B
2018
110
2000
B
2019
130
4000
B
2020
150
4500
C
2018
150
1000
C
2019
110
3000
C
2020
175
3000
我像这样使用了 Pandas 的 melt 函数
df.melt(id_vars = ['Items'], var_name="年份", value_name="价格")
但我很难为价格和销售额获取单独的列,因为它在一列中给出了价格和销售额。谢谢
让我们试试pandaswide_to_long
pd.wide_to_long(df, i='Items', j='year',
stubnames=['Price', 'Sales'],
suffix=r'\d+', sep=' in ').sort_index()
Price Sales
Items year
A 2018 100 5000
2019 120 6000
2020 135 6500
B 2018 110 2000
2019 130 4000
2020 150 4500
C 2018 150 1000
2019 110 3000
2020 175 3000
我的数据集包含一些关于不同年份的价格和销售额的信息。问题是每年的价格和销售额实际上都是不同的 header 列。例如 CSV 看起来像
Items | Price in 2018 | Price in 2019 | Price in 2020 | Sales in 2018 | Sales in 2019 | Sales in 2020 |
---|---|---|---|---|---|---|
A | 100 | 120 | 135 | 5000 | 6000 | 6500 |
B | 110 | 130 | 150 | 2000 | 4000 | 4500 |
C | 150 | 110 | 175 | 1000 | 3000 | 3000 |
我想给它展示这样的东西
Items | Year | Price | Sales |
---|---|---|---|
A | 2018 | 100 | 5000 |
A | 2019 | 120 | 6000 |
A | 2020 | 135 | 6500 |
B | 2018 | 110 | 2000 |
B | 2019 | 130 | 4000 |
B | 2020 | 150 | 4500 |
C | 2018 | 150 | 1000 |
C | 2019 | 110 | 3000 |
C | 2020 | 175 | 3000 |
我像这样使用了 Pandas 的 melt 函数 df.melt(id_vars = ['Items'], var_name="年份", value_name="价格")
但我很难为价格和销售额获取单独的列,因为它在一列中给出了价格和销售额。谢谢
让我们试试pandaswide_to_long
pd.wide_to_long(df, i='Items', j='year',
stubnames=['Price', 'Sales'],
suffix=r'\d+', sep=' in ').sort_index()
Price Sales
Items year
A 2018 100 5000
2019 120 6000
2020 135 6500
B 2018 110 2000
2019 130 4000
2020 150 4500
C 2018 150 1000
2019 110 3000
2020 175 3000