Home > Blockchain >  Filter on max date/time in pandas
Filter on max date/time in pandas

Time:10-14

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:

  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

CodePudding user response:

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

CodePudding user response:

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
  • Related