python pandas : group by several columns and count value for one column
<p>I have <strong>df</strong>:</p>

<pre><code>    orgs  feature1       feature2      feature3
0   org1        True        True         NaN
1   org1        NaN        True         NaN
2   org2        NaN        True         True 
3   org3        True        True       NaN
4   org4        True        True       True 
5   org4        True        True       True 
</code></pre>

<p>Now i would like count the number of distinct orgs per each feature. basically to have a <strong>df_Result</strong> like this:</p>

<pre><code>    features  count_distinct_orgs      
0   feature1        3        
1   feature2        4      
2   feature3        2        
</code></pre>

<p>Does anybody have an idea how to do that?</p>

<p>You can add <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.sum.html" rel="nofollow"><code>sum</code></a> to previous <a href="http://stackoverflow.com/a/40004637/2901002">solution</a>:</p>

<pre><code>df1 = df.groupby('orgs')
        .apply(lambda x: x.iloc[:,1:].apply(lambda y: y.nunique())).sum().reset_index()
df1.columns = ['features','count_distinct_orgs']

print (df1)
   features  count_distinct_orgs
0  feature1                    3
1  feature2                    4
2  feature3                    2
</code></pre>

<p>Another solution with <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.core.groupby.GroupBy.aggregate.html" rel="nofollow"><code>aggregate</code></a> <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.nunique.html" rel="nofollow"><code>Series.nunique</code></a>:</p>

<pre><code>df1 = df.groupby('orgs')
        .agg(lambda x: pd.Series.nunique(x))
        .sum()
        .astype(int)
        .reset_index()
df1.columns = ['features','count_distinct_orgs']
print (df1)
   features  count_distinct_orgs
0  feature1                    3
1  feature2                    4
2  feature3                    2
</code></pre>

<hr>

<p>Solution with <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.stack.html" rel="nofollow"><code>stack</code></a> works, but return warning:</p>

<blockquote>
  <p>C:\Anaconda3\lib\site-packages\pandas\core\groupby.py:2937: FutureWarning: numpy not_equal will not check object identity in the future. The comparison did not return the same result as suggested by the identity (<code>is</code>)) and will change.
    inc = np.r_[1, val[1:] != val[:-1]]</p>
</blockquote>

<pre><code>df1 = df.set_index('orgs').stack(dropna=False)
df1 = df1.groupby(level=[0,1]).nunique().unstack().sum().reset_index()
df1.columns = ['features','count_distinct_orgs']
print (df1)
   features  count_distinct_orgs
0  feature1                    3
1  feature2                    4
2  feature3                    2
</code></pre>

python pandas count group-by stack