DIEA
Pandas确实有一个内置的方法,between_time()来选择特定时间段内的行,但它只适用于datetime对象是索引(或者,最近,列)。由于您已经有一个 datetime 列,因此您可以像这样提取行(从此处调整示例):import pandas as pddata = [["20090102 04:51:00", 89.9900, 89.9900, 89.9900, 89.9900, 100], ["20190102 05:36:00", 90.0100, 90.0100, 90.0100, 90.0100, 200], ["20090102 05:44:00", 90.1400, 90.1400, 90.1400, 90.1400, 100], ["20090102 05:50:00", 90.0500, 90.0500, 90.0500, 90.0500, 500], ["20090102 05:56:00", 90.1000, 90.1000, 90.1000, 90.1000, 300], ["20090102 05:57:00", 90.1000, 90.1000, 90.1000, 90.1000, 200]]# Building sample dataframe with Datetime columndf = pd.DataFrame(data)df.columns = ["Datetime", "1", "2", "3", "4", "5"]df['Datetime'] = pd.to_datetime(df['Datetime'], format="%Y%m%d %H:%M:%S")# Extract rows with datetime matching index rangeprint(df.set_index("Datetime").between_time('5:30:00', '5:45:00'))这将仅输出时间范围之间的记录。 1 2 3 4 5Datetime 2019-01-02 05:36:00 90.01 90.01 90.01 90.01 2002009-01-02 05:44:00 90.14 90.14 90.14 90.14 100