Re: [问题] numpy array 一次选取两行

楼主: dododo (dododo)   2019-04-21 15:27:37
如果只是想要计算上下班时间差的话
可以不用两行两行读取计算
PT_record = [['AAA', '2019/03/08', 2, '17:54:43', '上班'],
['BBB', '2019/03/08', 2, '17:59:43', '上班'],
['AAA', '2019/03/08', 2, '23:02:54', '下班'],
['BBB', '2019/03/08', 2, '20:02:54', '下班'],
['AAA', '2019/03/13', 2, '17:45:20', '上班'],
['BBB', '2019/03/13', 2, '18:45:20', '上班'],
['AAA', '2019/03/13', 2, '22:11:00', '下班'],
['BBB', '2019/03/13', 2, '23:19:00', '下班']]
# 整理成dataframe
df = pd.DataFrame(PT_record)
keys = df.columns
values = ['id', 'time', 'nothing', 'end', 'status']
d = dict(zip(keys, values))
df = df.rename(columns = d)
df['end'] = pd.to_datetime(df['end'])
df = df.sort_values(['id','time'])
df = df.set_index(['id', 'time'])
# 利用pandas的groupby就可以达到时间差的效果
df = df.groupby(df.index)[['end']].diff()
df = df.dropna().rename(columns = {'end': '工作时间'})
如果练习的目的是要加强使用循环的技巧
那请忽略我吧XD

Links booklink

Contact Us: admin [ a t ] ucptt.com