以下是数据框(df),包括1月至8月期间的不同商店和日期时间
| datetime | shop | val |
|------------------|---------|-----|
| 04-07-2020 13:32 | ASSY#1 | 23 |
| 06-07-2020 07:25 | ASSY#1 | 22 |
| 06-07-2020 21:26 | BODY#1 | 22 |
| 07-07-2020 15:22 | ASSY#1 | 20 |
| 07-07-2020 19:55 | PAINT#1 | 22 |
| 07-07-2020 16:55 | ETM#1 | 60 |
输出:新列“break”
| datetime | shop | val | break |
|------------------|---------|-----|--------|
| 04-07-2020 13:32 | ASSY#1 | 23 | Tea |
| 06-07-2020 07:25 | ASSY#1 | 22 | Normal |
| 06-07-2020 21:26 | BODY#1 | 22 | Normal |
| 07-07-2020 15:22 | ASSY#1 | 20 | Normal |
| 07-07-2020 19:55 | PAINT#1 | 22 | Normal |
| 07-07-2020 16:55 | ETM#1 | 60 | Normal |
需要检查的条件,我有多家这样的商店
如果店铺为“Assy#1”且“日期时间”介于休息类别之间
| Break Category | Body Shop#1 | Paint#1 Shop | Assy#1 Shop |
|----------------|--------------|--------------|--------------|
| Tea | 8.53 ~9.00 | 8.53 ~9.00 | 8.53 ~9.00 |
| Tea | 13.30 ~13.37 | 13.30 ~13.37 | 13.30 ~13.37 |
| Tea | 17.23 ~17.30 | 17.23 ~17.30 | 17.23 ~17.30 |
| Tea | 22.30 ~22.37 | 22.30 ~22.37 | 22.30 ~22.37 |
| Lunch | 11.00 ~11.30 | 11.15 ~11.45 | 11.30 ~12.00 |
| Dinner | 20.00 ~20.30 | 20.15 ~20.45 | 20.30 ~21.00 |
| Supper | 02.20 ~02.40 | 02.40 ~3.00 | 02.40 ~03.00 |
| Tea | 05.00 ~05.17 | 05.00 ~05.17 | 05.00 ~05.17 |
我的代码
df['break'] = np.where((df['SHOP']= 'ASSY#1') & (df['TIME'] >= '08:53:00') & (df['TIME'] <= '08:59:59'), 'Tea',
np.where((df['SHOP']= 'ASSY#1') & (df['TIME'] >= '13:30:00') & (df['TIME'] <= '13:36:59'), 'Tea',
np.where((df['SHOP']= 'ASSY#1') & (df['TIME'] >= '17:23:00') & (df['TIME'] <= '17:29:59'), 'Tea',
np.where((df['SHOP']= 'ASSY#1') & (df['TIME'] >= '22:30:00') & (df['TIME'] <= '22:36:59'), 'Tea',
np.where((df['SHOP']= 'ASSY#1') & (df['TIME'] >= '11:30:00') & (df['TIME'] <= '11:59:59'), 'Lunch',
np.where((df['SHOP']= 'ASSY#1') & (df['TIME'] >= '20:30:00') & (df['TIME'] <= '20:59:59'), 'Dinner',
np.where((df['SHOP']= 'ASSY#1') & (df['TIME'] >= '02:40:00') & (df['TIME'] <= '02:59:59'), 'Supper',
np.where((df['SHOP']= 'ASSY#1') & (df['TIME'] >= '05:00:00') & (df['TIME'] <= '05:16:59'), 'Tea',
np.where((df['SHOP']= 'PAINT#1') & (df['TIME'] >= '08:53:00') & (df['TIME'] <= '08:59:59'), 'Tea',
np.where((df['SHOP']= 'PAINT#1') & (df['TIME'] >= '13:30:00') & (df['TIME'] <= '13:36:59'), 'Tea',
np.where((df['SHOP']= 'PAINT#1') & (df['TIME'] >= '17:23:00') & (df['TIME'] <= '17:29:59'), 'Tea',
np.where((df['SHOP']= 'PAINT#1') & (df['TIME'] >= '22:30:00') & (df['TIME'] <= '22:36:59'), 'Tea',
np.where((df['SHOP']= 'PAINT#1') & (df['TIME'] >= '11:15:00') & (df['TIME'] <= '11:44:59'), 'Lunch',
np.where((df['SHOP']= 'PAINT#1') & (df['TIME'] >= '20:15:00') & (df['TIME'] <= '20:44:59'), 'Dinner',
np.where((df['SHOP']= 'PAINT#1') & (df['TIME'] >= '02:40:00') & (df['TIME'] <= '02:59:59'), 'Supper',
np.where((df['SHOP']= 'PAINT#1') & (df['TIME'] >= '05:00:00') & (df['TIME'] <= '05:16:59'), 'Tea',
np.where((df['SHOP']= 'BODY#1') & (df['TIME'] >= '08:53:00') & (df['TIME'] <= '08:59:59'), 'Tea',
np.where((df['SHOP']= 'BODY#1') & (df['TIME'] >= '13:30:00') & (df['TIME'] <= '13:36:59'), 'Tea',
np.where((df['SHOP']= 'BODY#1') & (df['TIME'] >= '17:23:00') & (df['TIME'] <= '17:29:59'), 'Tea',
np.where((df['SHOP']= 'BODY#1') & (df['TIME'] >= '22:30:00') & (df['TIME'] <= '22:36:59'), 'Tea',
np.where((df['SHOP']= 'BODY#1') & (df['TIME'] >= '11:00:00') & (df['TIME'] <= '11:29:59'), 'Lunch',
np.where((df['SHOP']= 'BODY#1') & (df['TIME'] >= '20:00:00') & (df['TIME'] <= '20:29:59'), 'Dinner',
np.where((df['SHOP']= 'BODY#1') & (df['TIME'] >= '02:20:00') & (df['TIME'] <= '02:39:59'), 'Supper',
np.where((df['SHOP']= 'BODY#1') & (df['TIME'] >= '05:00:00') & (df['TIME'] <= '05:16:59'), 'Tea', 'Normal'))
这是最好的方法还是有其他有效的方法
这里有一个方法。解析小时数需要一些工作-请参见以下内容:
输出为:
相关问题 更多 >
编程相关推荐