Pandas Group to Divide by Max
<p>I'm trying to normalize a user count by dividing by the max users in each group. I'm able to get the results to calculate (commented out the print that works), but I'm having trouble getting the results to save back to the original table. The code below doesn't throw an error, but it also doesn't add any data to weeklyPerson:</p>

<pre><code>weeklyPersonGroups=weeklyPerson.groupby('Person')
PersonMax=weeklyPersonGroups['users'].max()
for name, group in weeklyPersonGroups:
    #print(weeklyPerson[weeklyPerson['Person']==name]['users']/PersonMax[name])
    weeklyPerson[weeklyPerson['Person']==name]['usersNorm']=weeklyPerson[weeklyPerson['Person']==name]['users']/PersonMax[name]
</code></pre>

<p>Use <code>groupby</code> and <code>transform</code></p>

<pre><code>weeklyPerson.groupby('Person').users.transform(lambda x: x / x.max())
</code></pre>

<p>Per @Jeff's suggestion</p>

<pre><code>weeklyPerson.users / weeklyPerson.groupby('Person').users.transform(np.max)
</code></pre>

<p>This avoids using <code>lambda</code> when it isn't necessary.</p>

python pandas