简体   繁体   English

如何在给定范围的Python表中添加特定日期的值?

[英]How to add the values for Specific days in Python Table for a given range?

I have a dataset (Product_ID,date_time, Sold) which has products sold on various dates. 我有一个数据集(Product_ID,date_time,Sold),其产品在不同日期出售。 The dates are being given for 9 months with random 13 days or more from a month. 这些日期为9个月,每月随机13天或更长时间。 I have to segregate the data in a such a way that the for each product how many products were sold daily 1-3 days, sold daily 4-7 given days, sold daily 8-15 given days and sold daily for >16 days. 我必须以这样的方式分离数据:每种产品每天销售多少产品1-3天,每天4-7天销售,每天8-15天销售,每天销售> 16天。 So how can I code this in python using pandas and other packages 那么如何使用pandas和其他包在python中编写代码呢?

PRODUCT_ID      DATE_LOCATION  Sold
0E4234          01-08-16 0:00    2
0E4234          02-08-16 0:00    7
0E4234          07-08-16 0:00    3
0E4234          08-08-16 0:00    1
0E4234          09-08-16 0:00    2
0E4234          10-08-16 0.00    1
.
. 
.
0G2342          22-08-16 0:00    1
0G2342          23-08-16 0:00    2
0G2342          26-08-16 0:00    1
0G2342          28-08-16 0:00    1
0G2342          29-08-16 0:00    3
0G2342          30-08-16 0:00    3
.
.
.(goes for 64 products each with 9 months of data)
.

I don't know even how to code for this in python The output needed is 我甚至不知道如何在python中编写代码所需的输出是

PRODUCT_ID      Days   Sold
0E4234          1-3      9 #(1,2) dates because range is 1 to 3
                4-7      7 #(7,8,9,10) dates because range is 4 to 7
                8-15     0
                 >16     0
0G2342          1-3      11 #(22,23),(26),(28,29,30) dates because range is 1 to 3
                4-7      0
                8-15     0
                 >16     0
.
.(for 64 products)
.

Would be happy if at least someone posted a link to where to start. 如果至少有人发布了从哪里开始的链接,那将会很高兴。 I tried 我试过了

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.groupby(["PRODUCT_ID","Days"]).Sold.sum()

This gave me the number of products sold between these days in each month.But I need the sum of the products for the specified range were the products are sold in a streak specified. 这给了我每个月这几天之间销售的产品数量。但我需要指定范围内的产品总和是产品以指定的条件出售。

Use transform for Series with same size as original DataFrame , binning with cut and aggregate sum : 对与原始DataFrame大小相同的Series使用transform ,使用cut和aggregate sum合并:

df['DATE_LOCATION'] = pd.to_datetime(df['DATE_LOCATION'], format='%d-%m-%y %H:%M')

df = df.sort_values("DATE_LOCATION")
s = (df["DATE_LOCATION"].diff().dt.days > 1).cumsum()

count = s.groupby(s).transform('size')
print (count)
0     2
1     2
2     4
3     4
4     4
5     4
6     2
7     2
8     1
9     3
10    3
11    3
Name: DATE_LOCATION, dtype: int32

bins = pd.cut(count, bins=[0,3,7,15,31], labels=['1-3', '4-7','8-15', '>=16'])
df = df.groupby(['PRODUCT_ID', bins])['Sold'].sum().reset_index()
print (df)
  PRODUCT_ID DATE_LOCATION  Sold
0     0E4234           1-3     9
1     0E4234           4-7     7
2     0G2342           1-3    11

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM