Chadi N
Chadi N

Reputation: 441

How to get the mean of the first and third quartile using Pandas?

This is what I did to use ascend a list by smallest to largest, then i found the median of the list. What I need now, is to find the mean of the first quartile, and of the third one.

import pandas as pd

f = pd.read_csv("data.csv")
print(f.CRIM.sort_values())
print(f['CRIM'].quantile([0.5]))

This code sort the values and gives me the median of the list.

Upvotes: 1

Views: 2710

Answers (2)

antonisange
antonisange

Reputation: 55

The 1st quartile splits off the lowest 25% of data from the highest 75%.

I am not pretty sure if there's a solution to get it instantly with a line of code but following code will make it for you.

data_first_quartile = f.loc[f['CRIM'] <= f['CRIM'].quantile(0.25))

print(data_first_quartile['CRIM'].mean())

Hope this works for you.

Upvotes: 0

AverageHomosapien
AverageHomosapien

Reputation: 708

Using dataframe.describe() will give the quartiles as well as various other pieces of useful information (mean, min, max, etc).

Edit

Link to docs: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.describe.html?highlight=describe#pandas.DataFrame.describe

Upvotes: 1

Related Questions