Pandas 过滤特定年份的数据框行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46878156/
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 04:40:32  来源:igfitidea点击:

Pandas filter dataframe rows with a specific year

pythonpandasdatetimedataframe

提问by user3494047

I have a dataframe dfand it has a Datecolumn. I want to create two new data frames. One which contains all of the rows from dfwhere the year equals some_yearand another data frame which contains all of the rows of dfwhere the year does not equal some_year. I know you can do df.ix['2000-1-1' : '2001-1-1']but in order to get all of the rows which are not in 2000 requires creating 2 extra data frames and then concatenating/joining them.

我有一个数据框df,它有一Date列。我想创建两个新的数据框。一个包含df年份等于的所有行some_year,另一个数据框包含df年份不等于的所有行some_year。我知道你可以这样做,df.ix['2000-1-1' : '2001-1-1']但为了获得所有不在 2000 年的行,需要创建 2 个额外的数据帧,然后连接/加入它们。

Is there some way like this?

有这样的方法吗?

include = df[df.Date.year == year]
exclude = df[df['Date'].year != year]

This code doesn't work, but is there any similar sort of way?

这段代码不起作用,但有没有类似的方法?

回答by Vaishali

You can use datetime accesor.

您可以使用日期时间访问器。

import datetime as dt
df['Date'] = pd.to_datetime(df['Date'])

include = df[df['Date'].dt.year == year]
exclude = df[df['Date'].dt.year != year]

回答by jezrael

You can simplify it by inverting mask by ~and for condition use Series.dt.yearwith intfor cast string year:

您可以通过反向掩码简化它~和条件使用Series.dt.yearint流延字符串year

mask = df['Date'].dt.year == int(year)
include = df[mask]
exclude = df[~mask]