【问题标题】:How to calculate summary statistics for certain consecutive day ranges如何计算某些连续日期范围的汇总统计数据
【发布时间】:2018-11-11 04:12:54
【问题描述】:

我有一个数据集 (DATE_LOCATION, Sold),其中包含在不同日期销售的产品。日期为 9 个月,从一个月开始随机 13 天或更长时间。我必须以这样一种方式分离数据,对于产品,有多少产品连续售出 1-3 天,连续售出 4-7 天,连续售出 8-15 天,连续售出> 16 天。那么如何使用 pandas 和其他包在 python 中编写代码

      DATE_LOCATION  Sold
      07-08-16 0:00    2
      08-08-16 0:00    7
      12-08-16 0:00    3
      13-08-16 0:00    1
      14-08-16 0:00    2
      15-08-16 0.00    1
      .
      . 
      .
      22-10-16 0:00    1
      23-10-16 0:00    2
      26-10-16 0:00    1
      28-10-16 0:00    1
      29-10-16 0:00    3
      30-10-16 0:00    3
      .
      .
      .(goes for 9 months of data)
      .

我什至不知道如何在 python 中为此编写代码 需要的输出是

 Days   Sold
 1-3     20 #(7,8),(22,23),(26),(28,29,30) dates because the range is [1,3]
 4-7      7 #(12,13,14,15) dates because the range is [4,7]
 8-15     0
  >16     0

如果至少有人发布了一个从哪里开始的链接,我会很高兴。 我试过了

df["DATE_LOCATION"] = pd.to_datetime(df.DATE_LOCATION)
df["DAY"] = df.DATE_LOCATION.dt.day
def flag(x):
    if 1<=x<=3:
        return '1-3'
    elif 4<=x<=7:
        return '4-7'
    elif 8<=x<=15:
        return '8-15'
    else:
        return '>=16'
df["Days"] = df.DAY.apply(flag)
df["Days"].Sold.sum()

这给了我每个月这些天之间售出的产品数量。但我需要指定范围内的产品总和,其中产品在指定的连续销售中。

【问题讨论】:

  • 这给了我每月 1-3,4-7,8-15,>16 天之间销售的产品数量。我需要连续 1-3 天、4-7 天的天数范围内销售的产品数量...
  • 我不需要两天之间销售的产品数量示例如果产品连续销售 2 天,我需要将其放置在 1-3 范围内,如果产品售价为接下来的 30 天,我需要将其放置在 >16 范围内。不取决于今天是什么日子

标签: python python-3.x pandas datetime


【解决方案1】:

我通过这段代码复制了输入数据

df = pd.DataFrame({'DATE_LOCATION': ['07-08-16 0:00', '08-08-16 0:00', '12-08-16 0:00',\
                                     '13-08-16 0:00', '14-08-16 0:00', '15-08-16 0:00',\
                                     '22-10-16 0:00', '23-10-16 0:00', '26-10-16 0:00',\
                                     '28-10-16 0:00', '29-10-16 0:00', '30-10-16 0:00',],\
                   'Sold': [2, 7, 3, 1, 2, 1, 1, 2, 1, 1, 3, 3]})
df.DATE_LOCATION = pd.to_datetime(df.DATE_LOCATION, dayfirst=True)

现在数据是这样的

   DATE_LOCATION  Sold
0     2016-08-07     2
1     2016-08-08     7
2     2016-08-12     3
3     2016-08-13     1
4     2016-08-14     2
5     2016-08-15     1
6     2016-10-22     1
7     2016-10-23     2
8     2016-10-26     1
9     2016-10-28     1
10    2016-10-29     3
11    2016-10-30     3

获取行之间的间隔,计算运行长度(连续天数)并将它们分组,直到运行长度继续延长,最后得到最大运行长度并汇总每组中已售商品的总和。

df['Day_Interval'] = df.DATE_LOCATION.diff().shift(0).fillna(0)

# calculate run length
day_intervals = (df.Day_Interval.values / np.timedelta64(1, 'D')).astype(int)
run_lengths = []
run_length = 0
groups = []
group = 0

for day_interval in day_intervals:
    if day_interval != 1:
        run_length = 1
        group += 1
        groups.append(group)
    else:
        run_length += 1
        groups.append(group)
    run_lengths.append(run_length)

df['Run_Length'] = run_lengths
df['Group'] = groups

# calculate summary statistic by group
df = df.groupby('Group')['Sold', 'Run_Length'].agg({'Sold': np.sum, 'Run_Length': np.max})
df['1-3'] = 0
df['4-7'] = 0
df['8-15'] = 0
df['>=16'] = 0

df.loc[(df.Run_Length >= 1) & (df.Run_Length <=3), "1-3"] = df.Sold
df.loc[(df.Run_Length >= 4) & (df.Run_Length <=7), "4-7"] = df.Sold
df.loc[(df.Run_Length >= 8) & (df.Run_Length <=15), "8-15"] = df.Sold
df.loc[(df.Run_Length >= 16), ">=16"] = df.Sold
df = df.T.iloc[2:]
df['Sold'] = df.sum(axis=1)
df = df[['Sold']]

输出(df):

Group   Sold
1-3     20
4-7     7
8-15    0
>=16    0

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-03
    • 1970-01-01
    • 2019-12-04
    • 2019-08-18
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多