首页 > 解决方案 > Pandas DataFrame 中的分组时间数据

问题描述

我有时间以这种格式给出,我想将它们分组为 6 个类别,如清晨、下午、晚上等。如何将时间格式更改为类别?Python中是否有任何内置库可以帮助我做到这一点

标签: pythonpandas

解决方案


我想我会这样做。

import pandas as pd

# obviously you need to import the values from an external file.
time_list = ["10:00:00", "13:30:00", "09:30:00", "10:22:00", "01:00:00"]
df = pd.DataFrame({"time":time_list})

# once imported make sure to let pandas know which column contains date time.
df['time'] = pd.to_datetime(df['time'])
df.groupby([df['time'].dt.hour])

# access the date time object 
df['hour'] = df['time'].dt.hour
df['hour'].replace({
    1:"night",
    9:"late morning",
    10:"late morning",
    13:"noon",

}, inplace=True)

这将返回包含“小时”列中所有类别的 Python 文件。类别必须定义为替换函数的字典类型参数。


推荐阅读