Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python:按多个列分组,并为一个列计算值_Python_Pandas_Count_Group By_Stack - Fatal编程技术网

python:按多个列分组,并为一个列计算值

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

我有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 
现在我想计算每个功能的不同组织的数量。基本上要有一个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