Python / Data Science Essentials Interview Questions
How do you compute descriptive statistics on a Pandas DataFrame?
Descriptive statistics summarise the central tendency, spread, and shape of a dataset. Pandas df.describe() is the starting point for any exploratory analysis, but knowing the individual methods gives you more precise control.
import pandas as pd import numpy as np df = pd.read_csv('housing.csv') # --- df.describe() --- # Numeric columns: count, mean, std, min, 25%, 50%, 75%, max df.describe() # Include object columns too df.describe(include='all') # --- Individual statistics --- df['price'].mean() # arithmetic mean df['price'].median() # 50th percentile â robust to outliers df['price'].mode()[0] # most frequent value (returns Series) df['price'].std() # standard deviation (ddof=1 by default) df['price'].var() # variance df['price'].skew() # skewness: >0 right-skewed, <0 left-skewed df['price'].kurt() # excess kurtosis (0 = normal dist) df['price'].quantile(0.90) # 90th percentile df['price'].quantile([0.25, 0.5, 0.75]) # multiple quantiles # IQR â interquartile range (robust measure of spread) Q1, Q3 = df['price'].quantile(0.25), df['price'].quantile(0.75) IQR = Q3 - Q1 # Outlier detection via IQR fence lower = Q1 - 1.5 * IQR upper = Q3 + 1.5 * IQR outliers = df[(df['price'] < lower) | (df['price'] > upper)] print(f'{len(outliers)} outliers detected ({len(outliers)/len(df)*100:.1f}%)')
More Related questions...