首页 > 解决方案 > 如何在熊猫数据框中将时间戳分类为晚上?

问题描述

我正在尝试将 pandas 数据帧时间戳分为早上、下午、晚上和晚上。

数据:

2020-02-28 11:40:00
2020-02-28 11:45:00
2020-02-28 11:50:00
2020-02-28 11:55:00
2020-02-28 12:00:00
2020-02-28 12:05:00
2020-02-28 12:10:00
2020-02-28 12:15:00
2020-02-28 12:20:00
2020-02-28 12:25:00
2020-02-28 12:30:00
2020-02-28 12:35:00
2020-02-28 12:40:00
2020-02-28 12:45:00
2020-02-28 12:50:00
2020-02-28 12:55:00
2020-02-28 13:00:00
2020-03-01 23:45:00
2020-03-01 23:50:00
2020-03-01 23:55:00
2020-03-02 00:00:00
2020-03-02 00:05:00
2020-03-02 00:10:00
2020-03-02 00:15:00
2020-03-02 00:20:00
2020-03-02 00:25:00
2020-03-02 00:30:00
2020-03-02 00:35:00
2020-03-02 00:40:00
2020-03-02 00:45:00
2020-03-02 00:50:00
2020-03-02 00:55:00
2020-03-02 01:00:00
2020-03-02 01:05:00

编码:

import pandas as pd
import numpy as np

data['date']= pd.to_datetime(data['date'])
data['day'] = np.nan
data['tod'] = np.nan

for i in range(len(data)):
    data['day'].loc[i] = data['date'].loc[i].strftime('%A')

for j in range(len(data)):
    t = int(data['date'].loc[j].strftime('%H'))
    if((t > 6) and (t < 12)):
        data['tod'].loc[j] = 'morning'
    elif((t > 12) and (t < 18)):
        data['tod'].loc[j] = 'afternoon'
    elif((t > 18) and (t < 00)):
        data['tod'].loc[j] = 'evening'
    elif((t > 00) and (t < 6)):
        data['tod'].loc[j] = 'night'
    else:
        continue

当我运行程序时,我得到以下输出:

第1部分 第2部分

if 语句未正确执行,它无法区分 12PM(00:00 军事时间)和 12AM。我需要的输出是从中午 12 点(军事时间 00 点)到晚上 18 点是“晚上”,从上午 12 点到早上 6 点是“晚上”

标签: pythonpandas

解决方案


我更喜欢使用pd.cut,但如果你想创建自定义函数,那么将来可能会有更复杂的逻辑:

def ftod(x):
    if (x>=0) & (x<6):
        tod = 'night'
    elif (x>=6) & (x<12):
        tod = 'morning'
    elif (x>=12) & (x<18):
        tod = 'afternoon'
    else:
        tod = 'evening'
    return tod

接下来将其映射到数据框:

data['tod'] = data.date.dt.hour.map(ftod) 

结果是:

                   date        tod
0   2020-02-28 11:40:00    morning
1   2020-02-28 11:45:00    morning
2   2020-02-28 11:50:00    morning
3   2020-02-28 11:55:00    morning
4   2020-02-28 12:00:00  afternoon
5   2020-02-28 12:05:00  afternoon
6   2020-02-28 12:10:00  afternoon
7   2020-02-28 12:15:00  afternoon
8   2020-02-28 12:20:00  afternoon
9   2020-02-28 12:25:00  afternoon
10  2020-02-28 12:30:00  afternoon
11  2020-02-28 12:35:00  afternoon
12  2020-02-28 12:40:00  afternoon
13  2020-02-28 12:45:00  afternoon
14  2020-02-28 12:50:00  afternoon
15  2020-02-28 12:55:00  afternoon
16  2020-02-28 13:00:00  afternoon
17  2020-03-01 23:45:00    evening
18  2020-03-01 23:50:00    evening
19  2020-03-01 23:55:00    evening
20  2020-03-02 00:00:00      night
21  2020-03-02 00:05:00      night
22  2020-03-02 00:10:00      night
23  2020-03-02 00:15:00      night
24  2020-03-02 00:20:00      night
25  2020-03-02 00:25:00      night
26  2020-03-02 00:30:00      night
27  2020-03-02 00:35:00      night
28  2020-03-02 00:40:00      night
29  2020-03-02 00:45:00      night
30  2020-03-02 00:50:00      night
31  2020-03-02 00:55:00      night
32  2020-03-02 01:00:00      night
33  2020-03-02 01:05:00      night

推荐阅读