Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Extract rows from dataframe of the last date

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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()]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading