python:按多个列分组,并为一个列计算值
我有df:python:按多个列分组,并为一个列计算值,python,pandas,count,group-by,stack,Python,Pandas,Count,Group By,Stack,我有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 Tr
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
现在我想计算每个功能的不同组织的数量。基本上要有一个df_结果,如下所示:
features count_distinct_orgs
0 feature1 3
1 feature2 4
2 feature3 2
有人知道怎么做吗?您可以添加到以前的:
另一个解决方案包括:
解决方案有效,但返回警告: C:\Anaconda3\lib\site packages\pandas\core\groupby.py:2937:FutureWarning:numpy not_equal将来不会检查对象标识。比较没有返回与标识(
is
)所建议的相同的结果,并且将更改。
inc=np.r_1;[1,val[1:::!=val[:-1]]
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
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
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