[英]Pandas Dataframe - Search by index
I have a dataframe where the index is a timestamp.我有一个数据框,其中索引是时间戳。
DATE VALOR
2020-12-01 00:00:00 0.00635
2020-12-01 01:00:00 0.00941
2020-12-01 02:00:00 0.01151
2020-12-01 03:00:00 0.00281
2020-12-01 04:00:00 0.01080
... ...
2021-04-30 19:00:00 0.77059
2021-04-30 20:00:00 0.49285
2021-04-30 21:00:00 0.49057
2021-04-30 22:00:00 0.50339
2021-04-30 23:00:00 0.48792
I´m searching for a specific date我正在寻找特定日期
drop.loc['2020-12-01 04:00:00']
VALOR 0.0108
Name: 2020-12-01 04:00:00, dtype: float64
I want the return for the index of search above.我想要上面搜索索引的返回值。
In this case is line 5. After I want to use this value to do a slice in the dataframe在这种情况下是第 5 行。在我想使用这个值在数据帧中做一个切片之后
drop[:5]
Thanks!谢谢!
It looks like you want to subset drop
up to index '2020-12-01 04:00:00'
.看起来您想将子集
drop
到索引'2020-12-01 04:00:00'
。
Then simply do this: drop.loc[:'2020-12-01 04:00:00']
然后简单地这样做:
drop.loc[:'2020-12-01 04:00:00']
No need to manually get the line number.无需手动获取行号。
output:输出:
VALOR
DATE
2020-12-01 00:00:00 0.00635
2020-12-01 01:00:00 0.00941
2020-12-01 02:00:00 0.01151
2020-12-01 03:00:00 0.00281
2020-12-01 04:00:00 0.01080
If you really want to get the position:如果你真的想得到这个职位:
pos = drop.index.get_loc(key='2020-12-01 04:00:00') ## returns: 4
drop[:pos+1]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.