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)
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
