我有一个布尔数据帧。如果每一行是,让我们说一个“教室”,每一列标题是一个“学生id”。以教室为索引,我只想知道以下几点:
数据帧如下所示:
df =pd.DataFrame({'classroom_id':[1,2,3,4], 'student_1':[True,True,True,True], 'student_2':[True,True,False,False], 'student_3':[True,False,False,False], 'student_4':[False,False,False,True]})
df = df.set_index('classroom_id')
我只是不太知道如何查询我想要的,也就是问,例如:
How many times is student_1 and student_2 the ONLY true values?
或者
How many times is student_1 and student_2 the ONLY students in a classroom?
预期结果:
1 (classroom 2)
但是,我不想让它回来:
2 (classroom 1 and classroom 2)
我只是不知道该怎么问熊猫。。。 如果有任何问题,特别是我没有说清楚的事情,请告诉我
试试这个
您可以尝试使用布尔索引:
输出:
而且,要获得计数,可以使用
shape
:输出:
相关问题 更多 >
编程相关推荐