我从Excel读取销售交易表,我很想知道第一批商品在1小时内的销售数量。让A
成为销售报告,我想创建B
。
A=
item Location time
X Canada 10:03:18
X Canada 10:08:38
X Canada 10:24:46
X Canada 11:16:35
X US 10:00:16
X US 11:52:12
Y Canada 2:08:38
Y Canada 4:01:48
Y US 13:32:02
Y US 14:07:03
B=
item location first sale count
X Canada 10:03:18 3
X US 10:00:16 1
Y Canada 2:08:38 1
Y US 13:32:02 2
这就是我所做的:
A= A.sort('time', ascending=True).reset_index()
sale_loc= pd.DataFrame(A.groupby(['item', 'Location'], sort = False).first()).reset_index()
for i in sale_loc.index:
sale_cutoff = (A.time[i] + dt.timedelta(hours=1)).time
但我得到时间操纵部分的错误。我尝试了不同的功能,我也尝试添加一个新的A列(时间+ 1小时)而不是循环,但类似的问题......
答案 0 :(得分:1)
而不是生成整个代码,我专注于你所说的抛出错误的部分。这是一个向您列出的时间添加一小时的工作示例:
sale_time = ['10:03:18', '10:08:38', '11:16:35', '10:00:16']
import datetime
for i in sale_time:
sale_time1 = datetime.time(hour = int(i[0:2]), minute=int(i[3:5]), second=int(i[6:8]))
print(sale_time1)
sale_cutoff = datetime.time(sale_time1.hour+1, sale_time1.minute, sale_time1.second)
print(sale_cutoff)
答案 1 :(得分:0)
import numpy as np
import pandas as pd
df = pd.DataFrame({'Location': ['Canada', 'Canada', 'Canada', 'Canada', 'US', 'US', 'Canada', 'Canada', 'US', 'US'], 'item': ['X', 'X', 'X', 'X', 'X', 'X', 'Y', 'Y', 'Y', 'Y'], 'time': ['10:03:18', '10:08:38', '10:24:46', '11:16:35', '10:00:16', '11:52:12', '2:08:38', '4:01:48', '13:32:02', '14:07:03']})
df['start'] = pd.to_datetime(df['time'])
grouped = df.groupby(['item', 'Location'])
df['end'] = (grouped['start'].transform(lambda grp: grp.min()+pd.Timedelta(hours=1)))
df['mask'] = (df['start'] < df['end'])
result = grouped['mask'].sum()
print(result)
产量
item Location
X Canada 3.0
US 1.0
Y Canada 1.0
US 2.0
Name: mask, dtype: float64
主要想法是按item
和Location
分组,找到每个组的最短开始时间,然后再加1小时:
df['end'] = (grouped['start'].transform(lambda grp: grp.min()+pd.Timedelta(hours=1)))
transform
返回与df
长度相同的系列,因此每行都会获得一个值:
In [319]: df
Out[319]:
Location item time start end
0 Canada X 10:03:18 2016-05-06 10:03:18 2016-05-06 11:03:18
1 Canada X 10:08:38 2016-05-06 10:08:38 2016-05-06 11:03:18
2 Canada X 10:24:46 2016-05-06 10:24:46 2016-05-06 11:03:18
3 Canada X 11:16:35 2016-05-06 11:16:35 2016-05-06 11:03:18
4 US X 10:00:16 2016-05-06 10:00:16 2016-05-06 11:00:16
5 US X 11:52:12 2016-05-06 11:52:12 2016-05-06 11:00:16
6 Canada Y 2:08:38 2016-05-06 02:08:38 2016-05-06 03:08:38
7 Canada Y 4:01:48 2016-05-06 04:01:48 2016-05-06 03:08:38
8 US Y 13:32:02 2016-05-06 13:32:02 2016-05-06 14:32:02
9 US Y 14:07:03 2016-05-06 14:07:03 2016-05-06 14:32:02
现在您可以轻松识别感兴趣的行。它们是start
小于end
的那些:
In [320]: df['mask'] = (df['start'] < df['end'])
In [321]: df
Out[321]:
Location item time start end mask
0 Canada X 10:03:18 2016-05-06 10:03:18 2016-05-06 11:03:18 True
1 Canada X 10:08:38 2016-05-06 10:08:38 2016-05-06 11:03:18 True
2 Canada X 10:24:46 2016-05-06 10:24:46 2016-05-06 11:03:18 True
3 Canada X 11:16:35 2016-05-06 11:16:35 2016-05-06 11:03:18 False
4 US X 10:00:16 2016-05-06 10:00:16 2016-05-06 11:00:16 True
5 US X 11:52:12 2016-05-06 11:52:12 2016-05-06 11:00:16 False
6 Canada Y 2:08:38 2016-05-06 02:08:38 2016-05-06 03:08:38 True
7 Canada Y 4:01:48 2016-05-06 04:01:48 2016-05-06 03:08:38 False
8 US Y 13:32:02 2016-05-06 13:32:02 2016-05-06 14:32:02 True
9 US Y 14:07:03 2016-05-06 14:07:03 2016-05-06 14:32:02 True
按item
和Location
再次分组,通过总结每个组的mask
为真的次数来找到所需的结果:
result = grouped['mask'].sum()