Pandas groupwise percentage
<p>How can I calculate a group-wise percentage in pandas?</p>

<p>similar to 
<a href="http://stackoverflow.com/questions/23627782/pandas-groupby-size-and-percentages">Pandas: .groupby().size() and percentages</a> or <a href="http://stackoverflow.com/questions/29299078/pandas-very-simple-percent-of-total-size-from-group-by">Pandas Very Simple Percent of total size from Group by</a> I want to calculate the percentage of a value per group.</p>

<p>How can I achieve this?</p>

<p>My dataset is structured like</p>

<pre><code>ClassLabel, Field
</code></pre>

<p>Initially, I aggregate on both <code>ClassLbel</code> and <code>Field</code> like</p>

<pre><code>grouped = mydf.groupby(['Field', 'ClassLabel']).size().reset_index()
grouped = grouped.rename(columns={0: 'customersCountPerGroup'})
</code></pre>

<p>Now I would like to know the percentage of customers in each group on a per group basis. The groups total can be obtained like <code>mydf.groupby(['Field']).size()</code> but I neither can merge that as a column nor am I sure this is the right approach - there must be something simpler.</p>

<h1>edit</h1>

<p>I want to calculate the percentage only based on a single group e.g. 3 0 0.125 1 0.250 the sum of 0 + 1 --> 0.125 + 0.250 = 0,375 and use this value to devide / normalize grouped and not grouped.sum()
<a href="http://i.stack.imgur.com/XpiVo.jpg" rel="nofollow"><img src="http://i.stack.imgur.com/XpiVo.jpg" alt="enter image description here"></a></p>

<p>IIUC you can use:</p>

<pre><code>mydf = pd.DataFrame({'Field':[1,1,3,3,3],
                   'ClassLabel':[4,4,4,4,4],
                   'A':[7,8,9,5,7]})

print (mydf)
   A  ClassLabel  Field
0  7           4      1
1  8           4      1
2  9           4      3
3  5           4      3
4  7           4      3

grouped = mydf.groupby(['Field', 'ClassLabel']).size()
print (grouped)
Field  ClassLabel
1      4             2
3      4             3
dtype: int64

print (100 * grouped / grouped.sum())
Field  ClassLabel
1      4             40.0
3      4             60.0
dtype: float64
</code></pre>

<hr>

<pre><code>grouped = mydf.groupby(['Field', 'ClassLabel']).size().reset_index()
grouped = grouped.rename(columns={0: 'customersCountPerGroup'})
print (grouped)
   Field  ClassLabel  customersCountPerGroup
0      1           4                       2
1      3           4                       3

grouped['per'] = 100 * grouped.customersCountPerGroup / grouped.customersCountPerGroup.sum()
print (grouped)
   Field  ClassLabel  customersCountPerGroup   per
0      1           4                       2  40.0
1      3           4                       3  60.0
</code></pre>

<p>EDIT by comment:</p>

<pre><code>mydf = pd.DataFrame({'Field':[1,1,3,3,3,4,5,6],
                   'ClassLabel':[0,0,0,1,1,0,0,6],
                   'A':[7,8,9,5,7,5,6,4]})

print (mydf)

grouped = mydf.groupby(['Field', 'ClassLabel']).size()
df =  grouped / grouped.sum()

df = (grouped / df.groupby(level=0).transform('sum')).reset_index(name='new')
print (df)
   Field  ClassLabel       new
0      1           0  8.000000
1      3           0  2.666667
2      3           1  5.333333
3      4           0  8.000000
4      5           0  8.000000
5      6           6  8.000000
</code></pre>

python pandas group-by percentage