Python / Data Science Essentials Interview Questions
How do you use value_counts() and pd.crosstab() to understand categorical data?
Categorical columns are understood by counting their frequencies and cross-tabulating them against other variables. These two tools answer the questions 'what values exist and how often?' and 'how are two categorical variables related?'
import pandas as pd tips = pd.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/tips.csv') # --- value_counts --- tips['day'].value_counts() # Sat 87 Fri 19 Sun 76 Thur 62 tips['day'].value_counts(normalize=True).round(3) # proportions: Sat 0.357, Sun 0.312, Thur 0.255, Fri 0.078 tips['day'].value_counts(dropna=False) # includes NaN count if any # Count unique values tips['day'].nunique() # 4 # Histogram of numeric with bins pd.cut(tips['total_bill'], bins=5).value_counts().sort_index() # --- pd.crosstab --- # Frequency cross-table: how many smokers vs non-smokers per day ct = pd.crosstab(tips['day'], tips['smoker']) # smoker No Yes # day # Fri 4 15 # Sat 45 42 # Sun 57 19 # Thur 45 17 # Proportions within rows (what % of each day are smokers) pd.crosstab(tips['day'], tips['smoker'], normalize='index').round(3) # With aggregation (mean tip by day and smoker) pd.crosstab(tips['day'], tips['smoker'], values=tips['tip'], aggfunc='mean').round(2)
More Related questions...