Python 数据帧中的组时间数据

Python 数据帧中的组时间数据,python,pandas,Python,Pandas,我想我会这样做 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&quo

我想我会这样做

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文件。必须将类别定义为replace函数的dict类型参数。

签出并同时签出。