在日期过滤 pandas 数据框 [英] filtering pandas dataframes on dates
问题描述
本质上,我只需要保留在未来两个月内的行。
什么是实现这个的最好方法。
如果是索引,你应该使用 .ix
或 .loc
selector。
例如: p>
df.ix ['2014-01-01':'2014-02-01']
查看详情 http://pandas.pydata.org/pandas-docs/stable/dsintro.html#indexing-selection
我想这很聪明在开始之前,大量阅读熊猫,因为它是一个非常强大的图书馆。
更新:
如果列不是索引,您有两个选择:
- 使其成为索引(如果是时间序列数据,则将其定为永久性)
-
df [(df ['date']>'2013-01-01')& (df ['date']<'2013-02-01')]
一个href =http://pandas.pydata.org/pandas-docs/stable/indexing.html#boolean-indexing>这里的一般说明
I have a pandas data frame with a 'date' column. Now i need to filter out all rows in the dataframe that have dates outside of the next two months. Essentially, I only need to retain the row that are within the next two months. What is the best way to achieve this.
If it's the index, you should use the .ix
or .loc
selector.
For example:
df.ix['2014-01-01':'2014-02-01']
See details here http://pandas.pydata.org/pandas-docs/stable/dsintro.html#indexing-selection
I guess it's smart to read up on pandas quite extensively before you start, as it's a very powerful library. Otherwise it will be hard to get anything meaningful done.
UPDATE:
If the column is not the index you have two choices:
- Make it the index (either temporarily or permanently if it's time-series data)
df[(df['date'] > '2013-01-01') & (df['date'] < '2013-02-01')]
See here for the general explanation
这篇关于在日期过滤 pandas 数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!