Search code examples
pandasconditional-statementsmedian

Pandas dataframe median of a column with condition


So I have a dataframe with two columns (price, location). Now I want to get the median of price, if the location is e.g. "Paris". How do I achieve that?

dataframe:

location   price    
paris       5    
paris       2    
rome        5    
paris       4

...

desired result: 4 (median of 2,5,4)


Solution

  • I think you need df.groupby to group on location, and then .median():

    median = df.groupby('location').median()
    

    To get the value for each location:

    median.loc['paris', 'price']
    

    Output:

    4