BARUN
BARUN

Reputation: 139

How to select maximum date from each month in Python3 Pandas

I've written below code to fetch date data from CSV file using Pandas.

import pandas as pd

data = pd.read_csv("data.csv")
print(data)

The output is mentioned below.

           dt
0  2020-09-24
1  2020-09-17
2  2020-10-29
3  2020-10-01
4  2020-10-08
5  2020-10-15
6  2020-10-22
7  2020-11-26
8  2020-11-05
9  2020-11-12

Now I wish to get maximum date for each month and the output should be as mentioned below.

           dt
0  2020-09-24
1  2020-10-29
2  2020-11-26

Upvotes: 1

Views: 1485

Answers (2)

Shubham Sharma
Shubham Sharma

Reputation: 71689

groupby on monthly period and agg the dt column using max:

df['dt'] = pd.to_datetime(df['dt'])
s = df.groupby(df['dt'].dt.to_period('M'), as_index=False).max()

    dt
0   2020-09-24
1   2020-10-29
2   2020-11-26

Upvotes: 2

sammywemmy
sammywemmy

Reputation: 28644

Convert dt to datetime :

df["dt"] = pd.to_datetime(df["dt"])

Resample data on month and get the max :

df.resample(on="dt", rule="M").max().reset_index(drop=True)

      dt
0   2020-09-24
1   2020-10-29
2   2020-11-26

Upvotes: 2

Related Questions