filter dates in python to only the first of the month

You can use groupby().transform() to extract the min dates, then use boolean indexing:

df.loc[df.groupby([pd.Grouper(key='date', freq='M'),'id'])
         .date.transform('min') == df['date']

That would give you the rows with minimum dates for each month. However, if you want exactly the first (e.g. oct 1st), you just need to check the day:


CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top