3

I have df:

    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 

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

    features  count_distinct_orgs      
0   feature1        3        
1   feature2        4      
2   feature3        2        

Does anybody have an idea how to do that?

1 Answer 1

2

You can add sum to previous solution:

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

Another solution with aggregate Series.nunique:

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

Solution with stack works, but return warning:

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 (is)) and will change. inc = np.r_[1, val[1:] != val[:-1]]

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.