Python Pandas 选择索引,其中索引大于 x
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24088795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python Pandas Select Index where index is larger than x
提问by user3092887
Say I have a DataFrame df
with date as index and some values. How can I select the rows where the date is larger than some value x
?
假设我有一个df
以日期为索引和一些值的 DataFrame 。如何选择日期大于某个值的行x
?
I know I can convert the index to a column and then do the select df[df['date']>x]
, but is that slower than doing the operation on the index?
我知道我可以将索引转换为列然后执行 select df[df['date']>x]
,但是这比对索引执行操作慢吗?
采纳答案by Datageek
Example of selecting from a DataFrame with the use of index:
使用索引从 DataFrame 中选择的示例:
from numpy.random import randn
from pandas import DataFrame
from datetime import timedelta as td
import dateutil.parser
d = dateutil.parser.parse("2014-01-01")
df = DataFrame(randn(6,2), columns=list('AB'), index=[d + td(days=x) for x in range(1,7)])
In [1]: df
Out[1]:
A B
2014-01-02 -1.172285 1.706200
2014-01-03 0.039511 -0.320798
2014-01-04 -0.192179 -0.539397
2014-01-05 -0.475917 -0.280055
2014-01-06 0.163376 1.124602
2014-01-07 -2.477812 0.656750
In [2]: df[df.index > dateutil.parser.parse("2014-01-04")]
Out[2]:
A B
2014-01-05 -0.475917 -0.280055
2014-01-06 0.163376 1.124602
2014-01-07 -2.477812 0.656750
回答by ntg
The existing answer is correct, however if we are selecting based on the index, the second method from herewould be faster:
现有答案是正确的,但是如果我们根据索引进行选择,则此处的第二种方法会更快:
# Set index
df = df.set_index(df['date'])
# Select observations between two datetimes
df.loc[pd.TimeStamp('2002-1-1 01:00:00'):pd.TimeStamp('2002-1-1 04:00:00')]