Advertisement
elena1234

group data by age and race, calculate the proportion in Python

May 12th, 2022
1,021
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.42 KB | None | 0 0
  1. import numpy as np
  2. import pandas as pd
  3. import matplotlib.pyplot as plt
  4. import seaborn as sns
  5. from scipy import stats
  6.    
  7. da = pd.read_csv("C:/Users/eli/Desktop/YtPruboBEemdqA7UJJ_tgg_63e179e3722f4ef783f58ff6e395feb7_nhanes_2015_2016.csv")
  8.  
  9. da["agegrp"] = pd.cut(da.RIDAGEYR, [18, 30, 40, 50, 60, 70, 80])
  10. b = da.groupby(["RIAGENDR", "RIDRETH1"]).size().unstack().fillna(0).apply(lambda x: x/x.sum(), axis=1)
  11. print(b)
  12.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement