pythonbeginner
Value Counts with Normalisation
Compute frequency distributions and percentage breakdowns of categorical columns.
pythonPress ⌘/Ctrl + Shift + C to copy
import pandas as pd
import numpy as np
df = pd.DataFrame({'country': np.random.choice(['US','UK','DE','FR','JP'], 1000, p=[0.4,0.2,0.15,0.15,0.1]),'status': np.random.choice(['active','inactive','pending'], 1000)})
print('Counts:', df['country'].value_counts())
print('Percent:', df['country'].value_counts(normalize=True).map('{:.1%}'.format))
# Cross-breakdown
print(pd.crosstab(df['country'], df['status'], normalize='index').round(2))Use Cases
- data profiling
- categorical analysis
- segment breakdown
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
pythonbeginner
Pandas Cross-Tabulation (crosstab)
Compute frequency and proportion cross-tabulations between two categorical columns.
Best for: categorical analysis
#pandas#crosstab
pythonintermediate
Pareto / Cumulative Share Analysis
Calculate cumulative share (Pareto 80/20) of values for product or customer ranking analysis.
Best for: product analytics
#pandas#pareto
pythonbeginner
Pandas Rank with Tie-Breaking Methods
Apply different ranking strategies (min, dense, average) and handle ties in pandas.
Best for: leaderboards
#pandas#ranking
pythonbeginner
Pandas nlargest / nsmallest
Efficiently retrieve the N largest or smallest rows without sorting the full DataFrame.
Best for: top-N queries
#pandas#top-n