Home > Net >  Extract rows from dataframe of the last date
Extract rows from dataframe of the last date

Time:08-24

I have got a dataframe 'df' with a column 'HIST_DATUM' containing the end of each month. Let's say from 31.12.2021, 31.01.2022 ... until 31.07.2022 Do you know a code which extracts to a new dataframe 'df2' the rows from the last date, which would be in my example: all rows cointaining the date = 31.07.2022 in column date. Thank you for your reply

CodePudding user response:

First I would convert the column to the Datetime data type:

df["HIST_DATUM"]=pd.to_datetime(df["HIST_DATUM"], format="%d.%m.%Y")

Then you can extract the rows with the most recent ("max") date:

df2=df[df["HIST_DATUM"]==df["HIST_DATUM"].max()]
  • Related