提取 CSV 文件的特定行,使用 pandas 按时间戳进行索引

问题描述 投票:0回答:1

我有一个 csv 文件,打印时如下所示

CSV printed

令我困惑的是,有“时间”而不是索引。当我尝试访问密钥文件['时间']时,我当然会犯一个错误...我想要实现的是在我的csv文件中获取每天的'00:01:00'时间..抱歉,如果它没有得到应有的解释。

更新: 打印

file.index
后,这是输出:

DatetimeIndex(['2024-05-08 00:01:00', '2024-05-08 00:02:00', '2024-05-08 00:03:00', '2024-05-08 00:04:00', '2024-05-08 00:05:00', '2024-05-08 00:06:00', '2024-05-08 00:07:00', '2024-05-08 00:08:00', '2024-05-08 00:09:00', '2024-05-08 00:10:00', ... '2024-05-08 21:44:00', '2024-05-08 21:45:00', '2024-05-08 21:46:00', '2024-05-08 21:47:00', '2024-05-08 21:48:00', '2024-05-08 21:49:00', '2024-05-08 21:50:00', '2024-05-08 21:51:00', '2024-05-08 21:52:00', '2024-05-08 21:53:00'], dtype='datetime64[ns]',名称='时间',长度=1313,频率=无)

python pandas
1个回答
0
投票

我想要实现的是获取每天(日期时间)时间 00:01 的组。我遇到了这个解决方案:

# Group by the date part of the index and select the first row of each group
first_rows_00_01 = test.groupby(test.index.date).first()

# Access the row corresponding to 00:01:00 for each day
row_00_01 = first_rows_00_01.loc[first_rows_00_01.index + pd.Timedelta(hours=0, minutes=1)]

这正是我正在寻找的

© www.soinside.com 2019 - 2024. All rights reserved.