I have a dataframe df
and it has a Date
column. I want to create two new data frames. One which contains all of the rows from df
where
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]
You can simplify it by inverting mask by ~
and for condition use Series.dt.year with int
for cast string year
:
mask = df['Date'].dt.year == int(year)
include = df[mask]
exclude = df[~mask]