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

Return the max value in a new column for each repeating value in the other column

Background: I was hoping to generate a new column named: datasample based on another column named: end_bin from a table.

Question: Is there a way to return the max value in each row of the new column if the value is repeated in the previous column.

Expected result:

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

end_bin datasample
6 1
8 1
10 1
2 3
3 1
2 3
2 3

I couldnt find a method to do this in pandas, any help is appreciated:)

>Solution :

Your question is unclear, but it looks like you want the size per group:

df['datasample'] = df.groupby('end_bin')['end_bin'].transform('size')

Output:

   end_bin  datasample
0        6           1
1        8           1
2       10           1
3        2           3
4        3           1
5        2           3
6        2           3
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