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

first value from selective column with groupby and .first() function

Existing Dataframe :

Id       col_1        col_2       col_3         col_4
A         3             6          6              2
A         3             6          6              5
A         3             6          6              4
B         2             4          4              6
B         2             4          4              6

Expected Dataframe :

Id       col_1        col_2       col_3         
A         3             6          6              
B         2             4          4 

I am trying to find first Appearance of the value from the selective columns.
I know with new_df= df.groupby('Id')['col_1'].first().reset_index() we can get the first value , but is there a way to get first value for multiple column(Required column) at once

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 :

For aggregation add list after groupby:

cols = ['col_1','col_2','col_3']
new_df = df.groupby('Id', as_index=False, sort=False)[cols].first()
print (new_df)
  Id  col_1  col_2  col_3
0  A      3      6      6
1  B      2      4      4

Or solution without groupby with DataFrame.drop_duplicates and select columns by names (added Id column):

cols = ['Id','col_1','col_2','col_3']
new_df = df.drop_duplicates('Id')[cols]
print (new_df)
  Id  col_1  col_2  col_3
0  A      3      6      6
3  B      2      4      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