Filtering Pandas DataFrames on dates

前端 未结 12 1224
时光取名叫无心
时光取名叫无心 2020-11-22 16:15

I have a Pandas DataFrame 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

12条回答
  •  孤独总比滥情好
    2020-11-22 16:43

    If date column is the index, then use .loc for label based indexing or .iloc for positional indexing.

    For example:

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

    See details here http://pandas.pydata.org/pandas-docs/stable/dsintro.html#indexing-selection

    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

    Note: .ix is deprecated.

提交回复
热议问题