gpt4 book ai didi

python - 选择两个 DatetimeIndex 日期之间的行

转载 作者:太空宇宙 更新时间:2023-11-04 08:40:49 24 4
gpt4 key购买 nike

我有以下格式的 CSV 文件:

vm,time,LoadInt1
abc-webapp-02,2017-05-31 10:00:00,3.133333
abc-webapp-02,2017-05-31 10:05:00,0.000000
abc-webapp-02,2017-05-31 10:10:00,0.000000
abc-webapp-02,2017-05-31 10:15:00,0.000000
abc-webapp-02,2017-05-31 10:20:00,0.000000
abc-webapp-02,2017-05-31 10:25:00,0.000000
abc-webapp-02,2017-05-31 10:30:00,0.000000
abc-webapp-02,2017-05-31 10:35:00,0.000000
abc-webapp-02,2017-05-31 10:40:00,0.000000

我使用以下代码将 CSV 文件读入 DataFrame。日期被解析为索引(DatetimeIndex)

dateparse = lambda x: pd.datetime.strptime(x, '%Y-%m-%d %H:%M:%S')
df = pd.read_csv("my_file.csv", header=0, parse_dates=[1], index_col=1, date_parser=dateparse)

现在我尝试使用以下代码获取两个日期之间的所有行(实际 CSV 文件在下面提到的日期之间有大量行):

df.loc['2017-05-30' : '2017-05-31']

请注意,建议采用上述方法here .但是,它对我不起作用。因此,这可能不是重复的问题。

最佳答案

使用查询方法:

df = pd.read_csv("my_file.csv", index_col=1, parse_dates=True)

In [121]: df.query("'2017-05-30' <= index <= '2017-06-01'")
Out[121]:
vm LoadInt1
time
2017-05-31 10:00:00 abc-webapp-02 3.133333
2017-05-31 10:05:00 abc-webapp-02 0.000000
2017-05-31 10:10:00 abc-webapp-02 0.000000
2017-05-31 10:15:00 abc-webapp-02 0.000000
2017-05-31 10:20:00 abc-webapp-02 0.000000
2017-05-31 10:25:00 abc-webapp-02 0.000000
2017-05-31 10:30:00 abc-webapp-02 0.000000
2017-05-31 10:35:00 abc-webapp-02 0.000000
2017-05-31 10:40:00 abc-webapp-02 0.000000

关于python - 选择两个 DatetimeIndex 日期之间的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45084935/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com