【问题标题】:Grouping Pandas Dataframe by multiple columns in order to get specific values按多列对 Pandas Dataframe 进行分组以获得特定值
【发布时间】:2021-11-16 23:33:17
【问题描述】:

让我们描述一下我的问题。

我从数据库中获取了大量数据。例如它看起来像:

d = [
{'Tag': 'Weight', 'Value': 15, 'Product': 'Apple', 'Year': 2019 },
{'Tag': 'Weight', 'Value': 14, 'Product': 'Apple', 'Year': 2020 },
{'Tag': 'Weight', 'Value': 16, 'Product': 'Apple', 'Year': 2021 },
{'Tag': 'Weight', 'Value': 30, 'Product': 'Banana', 'Year': 2019 },
{'Tag': 'Weight', 'Value': 32, 'Product': 'Banana', 'Year': 2020 },
{'Tag': 'Weight', 'Value': 31, 'Product': 'Banana', 'Year': 2021 },
{'Tag': 'Weight', 'Value': 120, 'Product': 'Papaya', 'Year': 2019 },
{'Tag': 'Weight', 'Value': 140, 'Product': 'Papaya', 'Year': 2020 },
{'Tag': 'Weight', 'Value': 130, 'Product': 'Papaya', 'Year': 2021 },
{'Tag': 'Price', 'Value': 0.23, 'Product': 'Apple', 'Year': 2019 },
{'Tag': 'Price', 'Value': 0.23, 'Product': 'Apple', 'Year': 2020 },
{'Tag': 'Price', 'Value': 0.24, 'Product': 'Apple', 'Year': 2021 },
{'Tag': 'Price', 'Value': 0.81, 'Product': 'Banana', 'Year': 2019 },
{'Tag': 'Price', 'Value': 0.83, 'Product': 'Banana', 'Year': 2020 },
{'Tag': 'Price', 'Value': 0.9, 'Product': 'Banana', 'Year': 2021 },
{'Tag': 'Price', 'Value': 2.31, 'Product': 'Papaya', 'Year': 2019 },
{'Tag': 'Price', 'Value': 2.29, 'Product': 'Papaya', 'Year': 2020 },
{'Tag': 'Price', 'Value': 2.41, 'Product': 'Papaya', 'Year': 2021 }
]

我用这个命令创建了一个数据框:

df = pd.DataFrame(data = d)

那么数据如下:

     Tag    Value   Product Year
0   Weight  15.00   Apple   2019
1   Weight  14.00   Apple   2020
2   Weight  16.00   Apple   2021
3   Weight  30.00   Banana  2019
4   Weight  32.00   Banana  2020
5   Weight  31.00   Banana  2021
6   Weight  120.00  Papaya  2019
...

到目前为止一切顺利。现在我想对这个数据框进行排序和过滤以制作漂亮的图。例如,我想显示过去几年的价格(Tag == 'Price')。这意味着在我的 X 轴上我想拥有所有产品,在 y 轴上我有相应的价格。例如,我希望每年都有一个单独的数据集,并标有那一年。在此条形图中的示例中,我为每种产品获得 3 个条形,每个代表一年的价格。

使用 pandas 的最佳方法是什么?

目前我正在遍历所有数据,找到正确的数据并填充新数组,只是为了将新创建的数组放入我的绘图中。但这似乎不是理想的方式。

所以问题是,如何获得我的绘图轴?你是如何以最优雅的方式解决这个问题的?只用熊猫?可能吗?

我很兴奋,非常感谢

【问题讨论】:

    标签: python pandas sorting plot grouping


    【解决方案1】:

    将您的数据子集到 'Price' 行,然后用 pivot 重新整形,这样该组织就适合绘制条形图 - 每个产品的行和每年的列。

    dfp = (df[df['Tag'].eq('Price')]
              .pivot(index='Product', columns='Year', values='Value'))
    #Year     2019  2020  2021
    #Product                  
    #Apple    0.23  0.23  0.24
    #Banana   0.81  0.83  0.90
    #Papaya   2.31  2.29  2.41
    
    dfp.plot(kind='bar', rot=0, ec='k')
    

    【讨论】:

    • 哇!太感谢了。这正是我想要的?
    【解决方案2】:

    试试这个:

    import numpy as np
    import matplotlib.pyplot as plt
     
    # set width of bar
    barWidth = 0.25
    fig = plt.subplots(figsize =(12, 8))
     
    # set height of bar
    Apple = list(df[(df.Product=='Apple')&(df.Tag=='Price')].Value)
    Banana = list(df[(df.Product=='Banana')&(df.Tag=='Price')].Value)
    Papaya = list(df[(df.Product=='Papaya')&(df.Tag=='Price')].Value)
     
    # Set position of bar on X axis
    br1 = np.arange(len(Apple))
    br2 = [x + barWidth for x in br1]
    br3 = [x + barWidth for x in br2]
     
    # Make the plot
    plt.bar(br1, Apple, color ='r', width = barWidth,
            edgecolor ='grey', label ='Apple')
    plt.bar(br2, Banana, color ='g', width = barWidth,
            edgecolor ='grey', label ='Banana')
    plt.bar(br3, Papaya, color ='b', width = barWidth,
            edgecolor ='grey', label ='Papaya')
     
    # Adding Xticks
    plt.xlabel('Year', fontweight ='bold', fontsize = 25)
    plt.ylabel('Price', fontweight ='bold', fontsize = 25)
    plt.xticks([r + barWidth for r in range(len(Apple))],['2019','2020','2021'])
     
    plt.legend()
    plt.show()
    

    输出:


    【讨论】:

      猜你喜欢
      • 2014-05-15
      • 2013-12-06
      • 2020-08-30
      • 1970-01-01
      • 2017-08-26
      • 1970-01-01
      • 2020-12-06
      • 2016-09-14
      • 1970-01-01
      相关资源
      最近更新 更多