pandas 根据日期值过滤熊猫数据框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17073150/
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
filtering a pandas Dataframe based on date value
提问by black_13
I have the following data imported from a csv file using pandas read_csv:
我使用 pandas 从 csv 文件导入了以下数据read_csv:
instrument type from_date to_date
0 96000001 W/D & V/L 19951227 19960102
1 96000002 DEED TRUST 19951227 19960102
2 96000003 WARNTY DEED 19951228 19960102
3 96000004 DEED TRUST 19951228 19960102
4 96000005 W/D & V/L 19951228 19960102
I would like to select those rows that fit a date or date range. For instance I want to
select only those rows with the date 19951227in the from_datecolumn or select days that range from from_dateof 19951227to to_date19960102.
我想选择那些适合日期或日期范围的行。比如我想用日期,只选择那些行19951227中from_date,范围从柱或选择天from_date的19951227到to_date19960102。
How would I do this?
我该怎么做?
回答by Andy Hayden
Select those with a specific column:
选择具有特定列的那些:
In [11]: df[df['from_date'] == 19951227]
Out[11]:
instrument type from_date to_date
0 96000001 W/D & V/L 19951227 19960102
1 96000002 DEED TRUST 19951227 19960102
Or combine several queries (you can use |for or)
或组合多个查询(您可以使用|for or)
In [12]: df[(19951227 <= df['from_date']) & (df['to_date'] <= 19960102)]
Out[12]:
instrument type from_date to_date
0 96000001 W/D & V/L 19951227 19960102
1 96000002 DEED TRUST 19951227 19960102
2 96000003 WARNTY DEED 19951228 19960102
3 96000004 DEED TRUST 19951228 19960102
4 96000005 W/D & V/L 19951228 19960102
Worth noting that these columns are not datetime/Timestamp objects...
值得注意的是,这些列不是日期时间/时间戳对象...
To convert these columns to timestamps you could use:
要将这些列转换为时间戳,您可以使用:
In [21]: pd.to_datetime(df['from_date'].astype(str))
Out[21]:
0 1995-12-27 00:00:00
1 1995-12-27 00:00:00
2 1995-12-28 00:00:00
3 1995-12-28 00:00:00
4 1995-12-28 00:00:00
Name: from_date, dtype: datetime64[ns]
In [22]: df['from_date'] = pd.to_datetime(df['from_date'].astype(str))
In [23]: pd.to_datetime(df['from_date'].astype(str)) # do same for to_date
And query via stringrepresentation of the date:
并通过日期的字符串表示进行查询:
In [24]: df['1995-12-27' == df['from_date']]
Out[24]:
instrument type from_date to_date
0 96000001 W/D & V/L 1995-12-27 00:00:00 19960102
1 96000002 DEED TRUST 1995-12-27 00:00:00 19960102

