Daniel Abramov
Daniel Abramov

Reputation: 41

How to filter dataframe with selecting only last row of every hour?

Data is inconsistent.

I've tried to filter DataFrame with
df.Timestamp.dt.hour gives only hours,
df.Timestamp.dt.minute gives only minutes.

I need to filter for example every last entry of hour so 1:58, 2:54, 3:36, 4:44, etc.

I just need more efficient way, not explanation :)

Upvotes: 0

Views: 1028

Answers (1)

zipa
zipa

Reputation: 27869

I think this should work:

df.sort_values('Date').groupby([df['Date'].dt.date, df['Date'].dt.hour], as_index=False).last()

Upvotes: 1

Related Questions