简体   繁体   English

Python Pandas 将选择性列转换为行

[英]Python Pandas convert selective columns into rows

My dataset has some information about price and sales for different years.我的数据集有一些关于不同年份的价格和销售额的信息。 The problem is each year is actually a different column header for price and for sales as well.问题是每年实际上是价格和销售额的不同列标题。 For example the CSV looks like例如 CSV 看起来像

Items项目 Price in 2018 2018年价格 Price in 2019 2019年价格 Price in 2020 2020年价格 Sales in 2018 2018年销售额 Sales in 2019 2019年销售额 Sales in 2020 2020年销售额
A一种 100 100 120 120 135 135 5000 5000 6000 6000 6500 6500
B 110 110 130 130 150 150 2000 2000年 4000 4000 4500 4500
C C 150 150 110 110 175 175 1000 1000 3000 3000 3000 3000

I want to show it something like this我想展示这样的东西

Items项目 Year Price价格 Sales销售量
A一种 2018 2018年 100 100 5000 5000
A一种 2019 2019年 120 120 6000 6000
A一种 2020 2020年 135 135 6500 6500
B 2018 2018年 110 110 2000 2000年
B 2019 2019年 130 130 4000 4000
B 2020 2020年 150 150 4500 4500
C C 2018 2018年 150 150 1000 1000
C C 2019 2019年 110 110 3000 3000
C C 2020 2020年 175 175 3000 3000

I used melt function from Pandas like this df.melt(id_vars = ['Items'], var_name="Year", value_name="Price")我使用了 Pandas 的 Melt 函数,如 df.melt(id_vars = ['Items'], var_name="Year", value_name="Price")

But I'm struggling in getting separate columns for Price and Sales as it gives Price and Sales in one column.但是我正在努力为价格和销售额获得单独的列,因为它在一列中提供了价格和销售额。 Thanks谢谢

Let us try pandas wide_to_long让我们试试 pandas wide_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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM