简体   繁体   中英

python pandas : group by several columns and count value for one column

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM