larosploc
larosploc

Reputation: 19

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)

Upvotes: 1

Views: 346

Answers (2)

Walisson Cardoso
Walisson Cardoso

Reputation: 109

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())

Upvotes: 1

Emi OB
Emi OB

Reputation: 3300

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

Upvotes: 1

Related Questions