pythonbeginner

Value Counts with Normalisation

Compute frequency distributions and percentage breakdowns of categorical columns.

python
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.