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 the median of a column based on criteria within dataframe and insert this as a new column

I’m trying to look in a dataframe, and find the median of data within a column based on another column.

I have a dataframe with ‘zipcode’ data and ‘price’ data. I want to find the median of the ‘price’ based on the ‘zipcode’, and report it in a new column. When I run the program as is, I get a column that reports the median of the whole dataset, but I want to add the column such that we would get the median of each zip code reported. What is the piece I am missing?

”’

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

d = {'zipcode': [99516, 99516, 99516, 99516, 89507, 89507, 89507], 
    'price': [15000, 14000, 13000, 78000, 3000, 4000, 500]}
df = pd.DataFrame(data=d)

medians = df.groupby(['zipcode','price'])['price'].transform('median')

df['median'] = df['price'].median()
df 

”’

>Solution :

You should groupby with zip code only

df['median_cal'] = df.groupby('zipcode')['price'].transform('median')
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