【问题标题】:Groupby plot in year wise for different categories of other column pandas and seaborn or matplotlib针对不同类别的其他列 pandas 和 seaborn 或 matplotlib 的 Groupby 逐年绘制
【发布时间】:2020-04-16 12:41:31
【问题描述】:

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

Place        Bldng_Id    Num_Bed_Rooms     Contract_date   Rental_value
Bangalore    1           4                 2016-02-16      100
Bangalore    1           4                 2016-05-16      150
Bangalore    1           4                 2017-01-18      450
Bangalore    1           4                 2017-02-26      550
Bangalore    5           4                 2015-02-26      120
Bangalore    5           4                 2016-05-18      180
Bangalore    2           3                 2015-03-06      150
Bangalore    2           3                 2016-05-14      150
Bangalore    2           3                 2017-07-26      220
Bangalore    2           3                 2017-09-19      200
Chennai      3           4                 2016-02-16      100
Chennai      3           4                 2016-05-16      150
Chennai      3           4                 2017-01-18      450
Chennai      3           4                 2017-02-26      550
Chennai      4           3                 2015-03-06      150
Chennai      4           3                 2016-05-14      150
Chennai      4           3                 2017-07-26      220
Chennai      4           3                 2017-09-19      200
Chennai      6           3                 2018-07-26      250
Chennai      6           3                 2019-09-19      280

从上面我想准备下面的数据框。

预期输出:

Place          Num_Bed_Rooms     Year            Avg_Rental_value
Bangalore      3                 2015            150
Bangalore      3                 2016            150
Bangalore      3                 2017            210
Bangalore      4                 2015            120
Bangalore      4                 2016            143.3
Bangalore      4                 2017            500
Chennai        3                 2015            150
Chennai        3                 2016            150
Chennai        3                 2017            210
Chennai        3                 2018            250
Chennai        3                 2019            280
Chennai        4                 2016            150
Chennai        4                 2017            210

我尝试了以下代码来实现这一点。

df.groupby(['Place', 'Year', 'Num_Bed_Rooms']).Rental_value.mean()

但是上面不能正常工作。

根据上述预期输出,我想编写一个时间序列代码来分别预测每个案例的下一年rental_value。

【问题讨论】:

    标签: pandas scikit-learn time-series pandas-groupby


    【解决方案1】:

    如有必要,首先将值转换为日期时间:

    df['Contract_date'] = pd.to_datetime(df['Contract_date'])
    

    然后创建新列并传递给groupby:

    df['Year'] = df['Contract_date'].dt.year
    df1 = df.groupby(['Place', 'Num_Bed_Rooms','Year'], as_index=False).Rental_value.mean()
    

    或者通过Series:

    y = df['Contract_date'].dt.year.rename('Year')
    df1 = df.groupby(['Place', 'Num_Bed_Rooms', y], as_index=False).Rental_value.mean()
    

    print (df1)
            Place  Num_Bed_Rooms  Year  Rental_value
    0   Bangalore              3  2015    150.000000
    1   Bangalore              3  2016    150.000000
    2   Bangalore              3  2017    210.000000
    3   Bangalore              4  2015    120.000000
    4   Bangalore              4  2016    143.333333
    5   Bangalore              4  2017    500.000000
    6     Chennai              3  2015    150.000000
    7     Chennai              3  2016    150.000000
    8     Chennai              3  2017    210.000000
    9     Chennai              3  2018    250.000000
    10    Chennai              3  2019    280.000000
    11    Chennai              4  2016    125.000000
    12    Chennai              4  2017    500.000000
    

    【讨论】:

    • 谢谢,预测明年租金价值的想法会很有帮助
    • @ALI - 不确定,如果理解得很好,但你可以检查this
    • 准确性无关紧要,一次运行每个案例的代码是指数加权平均或其他
    • 非常感谢耶兹瑞尔。将检查并尝试实施。那就是我要找的那个。但我不确定自己能否实现。
    • @ALI - 嗯,我认为这应该是一个新问题,因为机器处理对我来说并不容易(老实说,我只专注于熊猫)
    猜你喜欢
    • 2019-08-22
    • 2014-02-28
    • 2020-03-04
    • 1970-01-01
    • 2020-08-26
    • 2017-11-24
    • 2014-10-06
    • 2020-06-20
    • 1970-01-01
    相关资源
    最近更新 更多