在日期过滤 pandas 数据框 [英] filtering pandas dataframes on dates

查看:99
本文介绍了在日期过滤 pandas 数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日期列的熊猫数据框。现在,我需要过滤出数据帧中的所有行,这些行在接下来的两个月之后都有日期。
本质上,我只需要保留在未来两个月内的行。
什么是实现这个的最好方法。

解决方案

如果是索引,你应该使用 .ix .loc selector。



例如: p>

  df.ix ['2014-01-01':'2014-02-01'] 

查看详情 http://pandas.pydata.org/pandas-docs/stable/dsintro.html#indexing-selection



我想这很聪明在开始之前,大量阅读熊猫,因为它是一个非常强大的图书馆。



更新:



如果列不是索引,您有两个选择:


  1. 使其成为索引(如果是时间序列数据,则将其定为永久性)

  2. 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:

  1. Make it the index (either temporarily or permanently if it's time-series data)
  2. df[(df['date'] > '2013-01-01') & (df['date'] < '2013-02-01')]

See here for the general explanation

这篇关于在日期过滤 pandas 数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆