【问题标题】:Groupby year and calculate the average and count the size in pandas按年分组并计算平均值并计算熊猫的大小
【发布时间】:2020-04-20 21:59:45
【问题描述】:

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

Contract_ID    Place         Contract_Date      Price
1              Bangalore     2018-10-25         100
2              Bangalore     2018-08-25         200
3              Bangalore     2019-10-25         300
4              Bangalore     2019-11-25         200
5              Bangalore     2019-10-25         400
6              Chennai       2018-10-25         100
7              Chennai       2018-10-25         200
8              Chennai       2018-10-25         100
9              Chennai       2018-10-25         300
10             Chennai       2019-10-25         400
11             Chennai       2019-10-25         600

从上面我想使用熊猫生成下表。

预期输出:

Place       Year     Number_of_Contracts    Average_Price   
Bangalore   2018     2                      150
Bangalore   2019     3                      300
Chennai     2018     4                      175
Chennai     2019     2                      500

【问题讨论】:

    标签: pandas pandas-groupby


    【解决方案1】:

    GroupBy.aggSeries.dt.year 创建的年份和新列名的元组一起使用:

    df['Contract_Date'] = pd.to_datetime(df['Contract_Date'])
    df1 = (df.groupby(['Place', df['Contract_Date'].dt.year.rename('Year')])['Price']
             .agg([('Number_of_Contracts','size'),('Average_Price','mean')])
             .reset_index())
    print (df1)
           Place  Year  Number_of_Contracts  Average_Price
    0  Bangalore  2018                    2            150
    1  Bangalore  2019                    3            300
    2    Chennai  2018                    4            175
    3    Chennai  2019                    2            500
    

    解决方案named aggregation,但是pandas 0.25+是必须的:

    df['Contract_Date'] = pd.to_datetime(df['Contract_Date'])
    df1 = (df.groupby(['Place', df['Contract_Date'].dt.year.rename('Year')])
             .agg(Number_of_Contracts=('Contract_ID','size'),
                  Average_Price=('Price','mean'))
             .reset_index())
    print (df1)
           Place  Year  Number_of_Contracts  Average_Price
    0  Bangalore  2018                    2            150
    1  Bangalore  2019                    3            300
    2    Chennai  2018                    4            175
    3    Chennai  2019                    2            500
    

    【讨论】:

      猜你喜欢
      • 2021-07-07
      • 1970-01-01
      • 2017-02-18
      • 1970-01-01
      • 1970-01-01
      • 2019-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多