dingaro
dingaro

Reputation: 2342

How to select rows where date is in index in Python Pandas DataFrame?

I have DataFrame in Pythonlike below where data is in index (we can name this column "date"):

enter image description here

and I would like to select all column of this DF where data in index is > than 01.01.2020, how can I do it? (be aware that date is in index).

Upvotes: 1

Views: 83

Answers (1)

jezrael
jezrael

Reputation: 862481

Use boolean indexing:

df.index = pd.to_datetime(df.index, dayfirst=True)
df1 = df[df.index > '2020-01-01']

Or:

df.index = pd.to_datetime(df.index, dayfirst=True)
df1 = df[df.index > pd.to_datetime('2020-01-01')]

Upvotes: 1

Related Questions