Home > Mobile >  Pandas dataframe median of a column with condition
Pandas dataframe median of a column with condition

Time:11-26

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)

CodePudding user response:

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

CodePudding user response:

import pandas as pd

# Build dataframe
data = [['Paris', 2], ['New York', 3], ['Rome', 4], ['Paris', 5], ['Paris', 4]]
df = pd.DataFrame(data, columns=['location', 'price'])

# Get paris only rows
df_paris = df[df['location'] == 'Paris']

# Print median
print(df_paris['price'].median())
  • Related