根据行类别创建 pandas 新列 + 将行转换为列 - 在 groupby 方法中

Create pandas new columns based on rows category + convert rows into column - in groupby method

我正在使用的数据集如下。我想根据行分类向 table 添加新列。同时,我希望每个月只有一行。我不确定是否应该使用 groupby。

Year    Month   Index   Humidity    Temperature Pressure        date
2019    1       High    100%            30 °C   1021 mbar       20191
2019    1       Low     28%             9 °C    1011 mbar       20191
2019    1       Average 65%             21 °C   1016 mbar       20191
2019    2       High    100%            32 °C   1020 mbar       20192
2019    2       Low     28%             10 °C   1008 mbar       20192
2019    2       Average 63%             18°C    1014 mbar       20192

因此输出值如下所示:

**Desired output**

Year    Month   HighHumidity    LowHumidity AverageHumidity HighTemperature LowTemperature  AverageTemperature  HighPressure    LowPressure     AveragePressure     date
2019      1         100%            28%         65%             30 °C               9 °C        21 °C           1021 mbar           1011 mbar       1016 mbar       20191
2019      2         100%            27%         63%             32 °C               10 °C       18 °C           1020 mbar           1008 mbar       1014 mbar       20192

我试验了以下代码。然而,索引列适用于所有其他列,但我只想在此操作中使用特定列。:

df = df.pivot(index='date', columns=['Index'])
df.columns = ['_'.join((col[1], col[0])) for col in df.columns]

        Average_Year    High_Year   Low_Year    Average_Month   High_Month  Low_Month   Average_Humidity    High_Humidity   Low_Humidity    Average_Temperature High_Temperature    Low_Temperature Average_Pressure    High_Pressure   Low_Pressure
date                                                            
20191   2019                2019    2019            1                   1       1           65%                 100%            28%             21 °C               30 °C               9 °C            1016 mbar           1021 mbar   1011 mbar
201910  2019                2019    2019            10                  10      10          81%                 100%            49%             29 °C               35 °C               22 °C           1011 mbar           1016 mbar   1007 mbar
201911  2019                2019    2019            11                  11      11          77%                 100%            49%             26 °C               33 °C               16 °C           1013 mbar           1017 mbar   1006 mbar
201912  2019                2019    2019            12                  12      12          77%                 100%            38%             21 °C               31 °C               10 °C           1016 mbar           1021 mbar   1012 mbar
20192   2019                2019    2019            2                   2       2           65%                 100%            28%             23 °C               32 °C               10 °C           1015 mbar           1020 mbar   1008 mbar

您可以将 YearMonth 添加到参数 index:

df = df.pivot(index=['Year','Month','date'], columns=['Index'])
df.columns = ['_'.join((col[1], col[0])) for col in df.columns]
df = df.reset_index()

#for correct order date column
df['date'] = df.pop('date')
print (df)
   Year  Month Average_Humidity High_Humidity Low_Humidity  \
0  2019      1              65%          100%          28%   
1  2019      2              63%          100%          28%   

  Average_Temperature High_Temperature Low_Temperature Average_Pressure  \
0                21°C             30°C             9°C         1016mbar   
1                18°C             32°C            10°C         1014mbar   

  High_Pressure Low_Pressure   date  
0      1021mbar     1011mbar  20191  
1      1020mbar     1008mbar  20192