elena1234

population proportion , agg and total n in Python

Jun 6th, 2022 (edited)
891
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.56 KB | None | 0 0
  1. dx = da[["SMQ020x", "RIAGENDRx"]].dropna()
  2. pd.crosstab(dx.SMQ020x, dx.RIAGENDRx)
  3.  
  4. # Recode SMQ020x from Yes/No to 1/0 into existing variable SMQ020x
  5. dx["SMQ020x"] = dx.SMQ020x.replace({"Yes": 1, "No": 0})
  6.  
  7. dz = dx.groupby("RIAGENDRx").agg({"SMQ020x": [np.sum, np.mean, np.size]}) # np.mean in this case is about smokers proportion
  8. dz.columns = ["Proportion of smokers", "Total n"]
  9. dz
  10.  
  11. ######################################################
  12. from statsmodels.stats import proportion
  13. females = proportion.proportion_confint(906,2972) # for 95% conf. interval
  14. females
Add Comment
Please, Sign In to add comment