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

Viewed 27

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.

1 Answers

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

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