NAHEEM OLANIYAN
NAHEEM OLANIYAN

Reputation: 153

Drop rows based on month of year in date column in pandas

I am trying to drop rows in the month of December, January, and February.

Note: I set the date as index.

df.drop(df.loc[(df.index.month==12) | (df.index.month==1) |   (df.index.month==2)])

Upvotes: 3

Views: 3658

Answers (1)

cs95
cs95

Reputation: 402333

You can use Series.isin:

# Boolean indexing.
# df = df.loc[~df.index.month.isin([12, 1, 2]), :] # For a copy.
df = df[~df.index.month.isin([12, 1, 2])]

# Equivalent code using df.drop.
df = df.drop(df.index[df.index.month.isin([12, 1, 2])])

Upvotes: 4

Related Questions