我有一个数据集(DATE_LOCATION,已售出),其中包含在不同日期销售的产品。这些日期为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()
这给了我每个月这几天之间销售的产品数量。但是我需要指定范围内的产品总和,其中产品以指定条纹出售。
答案 0 :(得分: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
获取行之间的间隔,计算运行长度(连续天数)并将它们分组,直到运行长度继续扩展,最后得到最大run_length并汇总每个组中已售商品的总和。
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
希望这有帮助。