Skip to content
Advertisement

Python pandas dataframe with daily data – keep first and last rows per month

I have a Python pandas dataframe that looks like this:

JavaScript

I want to keep the first and the last row per month. How can I do that? I tried using the following code:

JavaScript

but I don’t get the results I want.

Advertisement

Answer

pandas groupby operations don’t sort each group prior to aggregation, which is why 'first' and 'last' are not selecting the correct rows for you.

Additionally, you can use .resample('M') instead of a groupby on year & month.

JavaScript

This output doesn’t have the most usable format, so we can use a quick .stack to remedy it:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement