<p>如果您在“选择2013年9月至2008年5月期间的所有行”帖子中要求查找2008年至2013年期间的行,也可以在下面进行尝试
然后使用<a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.between.html" rel="nofollow noreferrer">pandas.Series.between</a>:</p>
<p>数据集借用自@jezrael。。你知道吗</p>
<p>用于演示的数据帧:</strong></p>
<pre><code>>>> stats_month_census_2
year month data
0 2008 April 1
1 2008 May 3
2 2008 June 4
3 2013 September 6
4 2013 October 5
5 2014 November 6
6 2014 December 7
</code></pre>
<p><strong>使用<code>pandas.Series.between()</code></strong></p>
<pre><code>>>> stats_month_census_2[stats_month_census_2['year'].between(2008, 2013, inclusive=True)]
year month data
0 2008 April 1
1 2008 May 3
2 2008 June 4
3 2013 September 6
4 2013 October 5
</code></pre>
<p>如果只是<code>datetime</code>格式的问题,您可以在下面尝试:</p>
<pre><code>>>> stats_month_census_2[stats_month_census_2['year'].between('2008-05', '2013-09', inclusive=True)]
year month data
1 2008-05-01 May 3
2 2008-06-01 June 4
3 2013-09-01 September 6
</code></pre>
<p><strong>使用DataFame.query查询:</strong></p>
<pre><code>>>> stats_month_census_2.query('"2008-05" <= year <= "2013-09"')
year month data
1 2008-05-01 May 3
2 2008-06-01 June 4
3 2013-09-01 September 6
</code></pre>
<p><strong>使用isin方法:</strong>选择两个日期之间的行</p>
<pre><code>>>> stats_month_census_2[stats_month_census_2['year'].isin(pd.date_range('2008-05-01', '2013-09-01'))]
year month data
1 2008-05-01 May 3
2 2008-06-01 June 4
3 2013-09-01 September 6
</code></pre>
<p>或者,你也可以像下面这样通过。。你知道吗</p>
<pre><code>>>> stats_month_census_2[stats_month_census_2['year'].isin(pd.date_range('2008-05', '2013-09'))]
year month data
1 2008-05-01 May 3
2 2008-06-01 June 4
3 2013-09-01 September 6
</code></pre>
<p><strong>使用基于索引开始和结束日期的切片方法。。你知道吗</p>
<pre><code>Start = stats_month_census_2[stats_month_census_2['year'] =='2008-05'].index[0]
End = stats_month_census_2[stats_month_census_2['year']=='2013-09'].index[0]
>>> stats_month_census_2.loc[Start:End]
year month data
1 2008-05-01 May 3
2 2008-06-01 June 4
3 2013-09-01 September 6
</code></pre>
<p><strong>注意:</strong>为了满足@jezrael在评论中提出的好奇心,我添加了如何将<code>year</code>列转换为datetime格式:</p>
<p>因为我们有下面的示例DataFrame,其中有两个不同的列<code>year</code>和<code>month</code>,其中year列只有years,month列是文本字符串格式,所以首先我们需要将字符串转换为int形式join,或者通过使用pandas <code>pd.to_datetime</code>方法将day指定为1将year&month添加到一起。你知道吗</p>
<pre><code>df
year month data
0 2008 April 1
1 2008 May 3
2 2008 June 4
3 2013 September 6
4 2013 October 5
5 2014 November 6
6 2014 December 7
</code></pre>
<p>上面是datetime转换之前的原始数据帧,因此,我将采用下面的方法,这是我在vi-So期间学习到的。你知道吗</p>
<p>1-首先将<code>month</code>名称转换为int形式,并将其分配给一个名为<code>Month</code>的新列,这样我们以后就可以使用它进行转换了。你知道吗</p>
<pre><code>df['Month'] = pd.to_datetime(df.month, format='%B').dt.month
</code></pre>
<p>第二,或者最后直接把年份列转换成一个适当的<code>datetime</code>格式,直接分配给<code>year</code>列本身,我们可以说这是一种就地的。你知道吗</p>
<pre><code>df['Date'] = pd.to_datetime(df[['year', 'Month']].assign(Day=1))
</code></pre>
<blockquote>
<p>Now the Desired DataFrame and <code>year</code> column is in datetime Form:</p>
</blockquote>
<pre><code>print(df)
year month data Month
0 2008-04-01 April 1 4
1 2008-05-01 May 3 5
2 2008-06-01 June 4 6
3 2013-09-01 September 6 9
4 2013-10-01 October 5 10
5 2014-11-01 November 6 11
6 2014-12-01 December 7 12
</code></pre>