'How would I go about finding the median of a row for each of another row in my Dataframe?

Sorry about the confusing phrasing, I have a dataframe with a variable called test_id. The dataframe looks something like this:

test_id number
100 1234
100 3132
100 2300
101 332
101 3242
101 33442
102 3212
102 111332
102 9842

I would like to find the median of the number row, but for each test id. I have tried a few ways but none have worked, and rounding has confused me.

I would like my final result to look like this:

test_id number
100 3132
101 3242
102 111332

Sorry if this is a simple question, I am relatively new to pandas and dataframes in general.



Solution 1:[1]

You can use .groupby() and then use .median():

df.groupby(["test_id"])["number"].median()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 BrokenBenchmark