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

Filter on max date/time in pandas

I have the following made up dataframe where I what to filter and return the data points based on the max date/time only for further analysis.

# importing pandas library
import pandas as pd
 
# Hits 
# importing pandas library
import pandas as pd
 
# Hits 
player_list = [['2022-10-12 12:10',50000,1],
               ['2022-10-12 12:10',51000,1],
               ['2022-10-12 17:10',51500,1],
               ['2022-10-12 17:10',53000,2],
               ['2022-10-13 12:11',57009,2],
               ['2022-10-13 12:11',53001,4],
               ['2022-10-13 17:10',56250,4],
               ['2022-10-13 17:10',54000,4]]
 
# creating a pandas dataframe
df = pd.DataFrame(
  player_list,columns = ['sanp_date',
                         'hits',
                         'state'])
df['sanp_date'] = df['sanp_date'].astype('datetime64[ns]')

# printing dataframe
print(df)
print()
 
# checking the type
print(df.dtypes)

Output:

  sanp_date            hits      state
0 2022-10-12 12:10:00  50000      1
1 2022-10-12 12:10:00  51000      1
2 2022-10-12 17:10:00  51500      1
3 2022-10-12 17:10:00  53000      2
4 2022-10-13 12:11:00  57009      2
5 2022-10-13 12:11:00  53001      4
6 2022-10-13 17:10:00  56250      4
7 2022-10-13 17:10:00  54000      4

sanp_date    datetime64[ns]
hits                  int64
state                 int64
dtype: object

Expected outcome I am trying to get to is:

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

  sanp_date            hits       state
0 2022-10-13 17:10:00  56250      4
1 2022-10-13 17:10:00  54000      4

Any help would be greatly appreciated.
Alan

>Solution :

You can just do this,

df[df.sanp_date == df.sanp_date.max()]

The output will be,

    sanp_date   hits    state
6   2022-10-13 17:10:00     56250   4
7   2022-10-13 17:10:00     54000   4
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