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

Find number of rows with values and latest date

I have dataframe like that:

pk_id date
123   2020-01-01
223   2020-01-02
123   2020-01-03
224   2020-01-04

and I want to find pk_id = 123 and pk_id = 223 with their latest date and count the amount of such rows.

I have the following code

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

idx = plan_df.groupby('pk_id')['date'].idxmax()
df = df.loc[idx] 
df = df.loc[df['pk_id'] == 123] 

that forms dataframe

pk_id   date
123    2020-01-03
223    2020-01-02

and now I find the number of rows

num = df.shape[0]

I believe it can be done in one line. Any ideas?

>Solution :

You can try

out = df[df['pk_id'].isin([123, 223])].groupby('pk_id', as_index=False)['date'].max()
print(out)

   pk_id        date
0    123  2020-01-03
1    223  2020-01-02
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