【问题标题】:Calculating lag features of mean of pandas column for previous 3 month, 6 month, 1 year and 2 years for this column in pandas计算pandas中此列前3个月、6个月、1年和2年的pandas列均值的滞后特征
【发布时间】:2021-05-24 09:09:01
【问题描述】:

您好,我有一个带有月份 (int)、年份(int)、product_id(string)、city_id(string)、customer_id(string) 和 sales(int) 的 pandas 数据框,我想先按特定 customer_product_city_month_year 组合进行分组,然后为所有组合获取前 3 个月、6 个月、1 年和 2 年的销售额列的平均值作为 pandas 数据框中的新列

如果有任何反馈,我将不胜感激 谢谢

【问题讨论】:

标签: python-3.x pandas dataframe pandas-groupby rolling-computation


【解决方案1】:

准备一堆数据:

import pandas as pd
import numpy as np

# sample data
dti = pd.date_range("2017-1-1", "2020-12-1", freq="MS")
products = ["cities{}".format(c) for c in range(1, 4)]
cities = ["cities{}".format(c) for c in range(1, 3)]
customers = ["customer{}".format(c) for c in range(1, 5)]

idx = pd.MultiIndex.from_product([dti.month, dti.year, products, cities, customers],
                                 names=["month", "year", "product_id", "city_id", "customer_id"])

df = pd.DataFrame({"sales": np.random.randint(0, 30, size=len(idx))}, index=idx).reset_index()
>>> df
       month  year product_id  city_id customer_id  sales
0          1  2017  products1  cities1   customer1     20
1          1  2017  products1  cities1   customer2     28
2          1  2017  products1  cities1   customer3      5
3          1  2017  products1  cities1   customer4     17
4          1  2017  products1  cities2   customer1     16
...      ...   ...        ...      ...         ...    ...
55291     12  2020  products3  cities1   customer4      6
55292     12  2020  products3  cities2   customer1      1
55293     12  2020  products3  cities2   customer2     21
55294     12  2020  products3  cities2   customer3     16
55295     12  2020  products3  cities2   customer4      3

将(年、月)转换为日期时间。使用日期操作更容易 之后:

df2 = df.assign(datetime=pd.to_datetime(df["year"].astype(str) + df["month"].astype(str), format="%Y%m"))
df2 = df2.drop(columns=["month", "year"]).set_index("datetime").sort_index()

cols = ["product_id", "city_id", "customer_id"]
out = []
for month in [3, 6, 12, 24]:
    start = df2.index.max() - pd.tseries.offsets.DateOffset(months=month)
    out.append(df2[start:].groupby(cols).mean().squeeze().rename("{}M".format(month)))

输出:

>>> pd.concat(out, axis="columns")
                                       3M         6M        12M        24M
product_id city_id customer_id
products1  cities1 customer1    15.161458  14.779762  14.788462  14.433333
                   customer2    14.479167  14.580357  14.706731  14.761667
                   customer3    13.666667  13.738095  14.229167  14.680000
                   customer4    14.828125  14.875000  14.641026  14.539167
           cities2 customer1    14.723958  14.211310  14.161859  14.440000
                   customer2    14.791667  15.083333  14.907051  14.781667
                   customer3    15.015625  14.273810  14.605769  14.664167
                   customer4    14.036458  14.505952  14.358974  14.267500
products2  cities1 customer1    13.666667  13.755952  14.166667  14.179167
                   customer2    13.484375  14.375000  14.025641  14.277500
                   customer3    13.645833  14.119048  14.307692  14.296667
                   customer4    13.244792  13.657738  14.112179  14.137500
           cities2 customer1    13.682292  14.541667  14.434295  14.366667
                   customer2    13.979167  14.116071  14.304487  14.592500
                   customer3    16.489583  15.744048  14.828526  14.671667
                   customer4    14.625000  14.505952  14.099359  14.409167
products3  cities1 customer1    13.562500  14.125000  14.149038  14.221667
                   customer2    14.713542  14.717262  14.520833  14.060833
                   customer3    14.562500  14.696429  14.560897  14.596667
                   customer4    14.536458  14.651786  14.314103  14.075833
           cities2 customer1    14.130208  14.559524  14.089744  14.365000
                   customer2    14.276042  14.279762  14.352564  14.273333
                   customer3    13.901042  14.657738  14.641026  14.254167
                   customer4    14.411458  14.303571  14.258013  14.265000

【讨论】:

  • groupby 和 concat 后如何将日期时间索引保留在数据框中?
【解决方案2】:

我收到一个错误,因为我还有一个 week 变量,所以我的数据框有点不同:

月 年 product_id city_id customer_id sales 0 1 2017 产品1 城市1 客户1 20 1 2 2017 产品1 城市1 客户1 28 2 3 2017 产品1 城市1 客户1 5 3 2 2017 产品1 城市2 客户2 28 4 1 2017 产品1 城市3 客户3 5

我得到的错误是 TypeError: Index(...) must be called with a collection of some kind, '3M' was passed 任何反馈都会非常感谢

【讨论】:

    猜你喜欢
    • 2019-05-22
    • 1970-01-01
    • 2021-12-08
    • 2015-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-28
    相关资源
    最近更新 更多