title: Pandas Custom Groupby Percentages of Total
body: <p>for a school project I need to implement the following function.</p>
  <p>Make a function <code>select(df, col1, col2)</code> that takes a data frame and two column labels and outputs a multi indexed Series with the fraction of occurrences of possible values of col2 given the values of col1.</p>
  <p>For example <code>select(df_test, 'Do you ever gamble?', 'Lottery Type')</code> would yield</p>
  <pre><code>No                   risk yes        0.433099
                       risk no         0.566901
  Yes                  risk yes        0.548872
                       risk no         0.451128
  </code></pre>
  <p>Note that the sum of Lottery Type:risk yes + Lottery Type:risk no is 1.0.</p>
  <p>It was a much larger dataframe but I managed to groupby and aggregate to a point using <code>gr = df.groupby([col1, col2], as_index=True).count()</code> It resulted in the below smallish dataframes. ;</p>
  <pre><code>        Do you ever smoke cigarettes?   Do you ever drink alcohol?  Have you ever been skydiving?   Do you ever drive above the speed limit?    Have you ever cheated on your significant other?    Do you eat steak?   How do you like your steak prepared?    Gender  Age Household Income    Education   Location (Census Region)
  Do you ever gamble? Lottery Type                                                
  No  risk no 155 157 156 157 155 157 121 147 147 121 147 145
  risk yes    120 120 120 119 120 120 89  117 117 94  116 117
  Yes risk no 114 114 113 113 114 114 99  110 110 96  109 110
  risk yes    141 142 141 142 142 141 116 133 133 113 133 133
  
  </code></pre>
  <p><a href="https://i.stack.imgur.com/t1qAL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/t1qAL.png" alt="The Code looks messy so this is an image of the above DF" /></a></p>
  <p>The Code looks messy so this is an image of the above DF. So my question is how can I aggregate on the percentage of the people say who don't smoke and percentage of the people who smoke. I tried using custom aggregation functions but I couldn't figure out. Using the below function just throws a type error.
  <code>.agg(lambda x: sum(x)/len(x))</code></p>
  <p><code>TypeError: unsupported operand type(s) for +: 'int' and 'str'</code></p>