How to do it...

  1. Define a function that returns the percentage of schools with an undergraduate population between 1,000 and 3,000:
>>> def pct_between_1_3k(s):
return s.between(1000, 3000).mean()
  1. Calculate this percentage grouping by state and religious affiliation:
>>> college.groupby(['STABBR', 'RELAFFIL'])['UGDS'] 
.agg(pct_between_1_3k).head(9)
STABBR RELAFFIL AK 0 0.142857 1 0.000000 AL 0 0.236111 1 0.333333 AR 0 0.279412 1 0.111111 AS 0 1.000000 AZ 0 0.096774 1 0.000000 Name: UGDS, dtype: float64
  1. This function works fine but it doesn't give the user any flexibility to choose the lower and upper bound. Let's create a new function that allows the user to define these bounds:
>>> def pct_between(s, low, high):
return s.between(low, high).mean()
  1. Pass this new function to the agg method along with lower and upper bounds:
>>> college.groupby(['STABBR', 'RELAFFIL'])['UGDS'] 
.agg(pct_between, 1000, 10000).head(9)
STABBR RELAFFIL AK 0 0.428571 1 0.000000 AL 0 0.458333 1 0.375000 AR 0 0.397059 1 0.166667 AS 0 1.000000 AZ 0 0.233871 1 0.111111 Name: UGDS, dtype: float64
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset