Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用loc和iloc组合选择索引_Python_Pandas_Dataframe_Indexing - Fatal编程技术网

Python 如何使用loc和iloc组合选择索引

Python 如何使用loc和iloc组合选择索引,python,pandas,dataframe,indexing,Python,Pandas,Dataframe,Indexing,我有一个按日期索引的时间排序数据集,如下所示: result 2009-09-08 Positive 2009-08-24 Negative 2009-06-01 Positive 2009-04-23 Positive 2008-12-06 Positive ... ... 我想找到一个日期,例如,2009-08-24加上日期下方的两行,即 2009-08-24 Negative 2009-06-01 Positive 2009-04

我有一个按日期索引的时间排序数据集,如下所示:

             result
2009-09-08  Positive
2009-08-24  Negative
2009-06-01  Positive
2009-04-23  Positive
2008-12-06  Positive
 ...         ...
我想找到一个日期,例如,2009-08-24加上日期下方的两行,即

2009-08-24  Negative
2009-06-01  Positive
2009-04-23  Positive
我的问题是,查找特定日期需要loc(df.loc['2009-08-24']),但查找该日期和下面两行需要数字位置(iloc)。在matlab中,我将首先找到“2009-08-24”(本例中的第二行)的数字行号“n”,然后选择行“n”到“n+2”。但我不确定python中是否有更简单的方法。坦斯克

怎么样

df.loc['2009-08-24':].head(2)
怎么样

df.loc['2009-08-24':].head(2)
将返回所需日期的“数字行号”。

将返回所需日期的“数字行号”