pandas 选择日期列的两个日期之间的数据框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50014369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:30:30  来源:igfitidea点击:

Select Data frame between two dates of a date column

pandasdatedataframe

提问by Niccola Tartaglia

I would like to subset a data frame based on a date column, which originally has this format:

我想根据日期列对数据框进行子集化,该列最初具有以下格式:

3/22/13

After I transform it to a date:

在我将其转换为日期后:

df['date']=pd.to_datetime(df['date'], format='%m/%d/%y')

I get this:

我明白了:

2013-03-22 00:00:00

Now I would like to subset it with something like this:

现在我想用这样的子集:

 df.loc[(df['date']>'2014-06-22')]

But that either gives me an empty data frame or full data frame, that is no filtering.

但这要么给了我一个空的数据框或一个完整的数据框,那就是没有过滤。

Any suggestions how I can get this to work?

有什么建议我可以让它发挥作用吗?

remark: I am well aware that similar questions have been asked in other forums but I could not figure out a solution since my date column looks different.

备注:我很清楚在其他论坛上也有人问过类似的问题,但由于我的日期列看起来不同,我无法找到解决方案。

回答by Charles R

First you have to convert your starting date and final date into a datetime format. Then you can apply multiple conditions inside df.loc. Do not forget to reassign your modifications to your df :

首先,您必须将开始日期和最终日期转换为日期时间格式。然后你可以在 df.loc 中应用多个条件。不要忘记将您的修改重新分配给您的 df :

import pandas as pd
from datetime import datetime

df['date']=pd.to_datetime(df['date'], format='%m/%d/%y')

date1 = datetime.strptime('2013-03-23', '%Y-%m-%d')
date2 = datetime.strptime('2013-03-25', '%Y-%m-%d')

df = df.loc[(df['date']>date1) & (df['date']<date2)]