【问题标题】:Find average of each求每个的平均值
【发布时间】:2022-01-23 06:22:19
【问题描述】:

我有一个如下所示的数据框:

id    name         industry               income
1     apple       telecommunication         100     
2     oil           gas                     100
3    samsung      telecommunication         200
4   coinbase       crypto                   100
5   microsoft    telecommunication          30

所以我想做的是找到每个行业的平均收入。 应该是:电信 110,gas 100,加密 100。

我所做的是找到每个行业的频率:

df.groupby(['industry']).sum().value_counts('industry')

导致:

industry
telecommunication       3
gas                     1
crypto                  1

我还找到了每个行业的收入总和:

df.groupby(['industry']).sum()['income']

导致

industry
telecommunication       330
gas                     100
crypto                  100

现在我有点不知道如何继续......

【问题讨论】:

    标签: pandas dataframe pandas-groupby


    【解决方案1】:

    你正在寻找mean:

    means = df.groupby('industry')['income'].mean()
    

    输出:

    >>> means
    industry
    crypto               100.0
    gas                  100.0
    telecommunication    110.0
    Name: income, dtype: float64
    
    >>> means['telecommunication']
    110.0
    

    【讨论】:

      【解决方案2】:

      如果你想保留所有其他细节,groupby 和 transform

      df['mean']=df.groupby('industry')['income'].transform('mean')
      
      
      
        id       name           industry  income   mean
      0   1      apple  telecommunication     100  110.0
      1   2        oil                gas     100  100.0
      2   3    samsung  telecommunication     200  110.0
      3   4   coinbase             crypto     100  100.0
      4   5  microsoft  telecommunication      30  110.0
      

      如果你需要一个总结框架

      df.groupby('industry')['income'].mean().to_frame('mean_income')
      
         
      
                           mean_income
      industry                      
      crypto                   100.0
      gas                      100.0
      telecommunication        110.0
      

      【讨论】:

        【解决方案3】:

        也许你应该使用agg 来避免多次操作:

        out = df.groupby('industry', sort=False).agg(size=('income', 'size'), 
                                                     mean=('income', 'mean'), 
                                                     sum=('income', 'sum')).reset_index()
        print(out)
        
        # Output:
                    industry  size   mean  sum
        0  telecommunication     3  110.0  330
        1                gas     1  100.0  100
        2             crypto     1  100.0  100
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-06-22
          • 2021-01-17
          • 2022-01-19
          • 2014-05-11
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多