Advertisement
elena1234

crosstab, pivot_table and groupby in Python

Mar 16th, 2023 (edited)
709
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.79 KB | None | 0 0
  1. pd.crosstab(index = titanic.sex, columns =  titanic.pclass, values=titanic.age, aggfunc="mean")
  2.  
  3. titanic.pivot_table(index = "sex", columns = "pclass", values = "age", aggfunc = "mean")
  4.  
  5. titanic.groupby(["sex", "pclass"]).age.mean().unstack()
  6.  
  7. titanic.groupby(["sex", "age"], as_index = False).mean()
  8.  
  9. ####################################################################################################
  10. pd.crosstab(index = [summer.Year, summer.Country], columns = summer.Medal, aggfunc = "count",
  11.             values = summer.Athlete, margins = True).fillna(0)
  12.  
  13. summer.pivot_table(index=["Year", "Country"], columns = "Medal", aggfunc = "count",
  14.                    values = "Athlete", fill_value=0, margins = True)
  15.  
  16. summer.groupby(["Year", "Country", "Medal"]).Medal.count().unstack(fill_value = 0)
  17.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement