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

Compare timestamps from a column and put 1 to another column for the latest timestamp in pandas

Lets say i have a dataframe like this:

     col1              col2
0    data1       2021-03-21 19:58:09
1    data1       2021-03-22 19:58:09
2    data1       2021-03-23 19:58:09
3    data2       2021-03-20 19:58:09
4    data2       2021-03-21 19:58:09
5    data2       2021-03-22 19:58:09
6    data3       2021-03-19 19:58:09
7    data3       2021-03-20 19:58:09
8    data1       2021-03-24 19:58:09

How can i compare for example all the timestamps in col2 from data1 in col1 and create another column where i put 1 for the latest timestamp and 0 for the others:

     col1              col2                latest
0    data1       2021-03-21 19:58:09          0
1    data1       2021-03-22 19:58:09          0
2    data1       2021-03-23 19:58:09          0
3    data2       2021-03-20 19:58:09          0
4    data2       2021-03-21 19:58:09          0
5    data2       2021-03-22 19:58:09          1
6    data3       2021-03-19 19:58:09          0
7    data3       2021-03-20 19:58:09          1
8    data1       2021-03-24 19:58:09          1

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

>Solution :

Do a groupby transform, and compare the values with the max:

df['col2'] = pd.to_datetime(df['col2'])
latest = df.col2 == df.groupby('col1', sort = False).col2.transform('max')
df.assign(latest = latest.astype(int))
 
    col1                col2  latest
0  data1 2021-03-21 19:58:09       0
1  data1 2021-03-22 19:58:09       0
2  data1 2021-03-23 19:58:09       0
3  data2 2021-03-20 19:58:09       0
4  data2 2021-03-21 19:58:09       0
5  data2 2021-03-22 19:58:09       1
6  data3 2021-03-19 19:58:09       0
7  data3 2021-03-20 19:58:09       1
8  data1 2021-03-24 19:58:09       1
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