计算“ true”数据框中的值
如何检查数据框中的选定列返回 true 以及如何将 true 值的计数与正在处理正在处理的单元格计数的列名计数的计数不是null 。
这些选定的列可以具有false作为值。
为此,我使用了以下语句,但我不知道如何将其与列 count 进行比较
df=pd.DataFrame({'dept':['dept1','dept2','dept3','dept4','dept5'],
'subd':['hndf','nbf','asx','qwe','def'],
'jju':['0','1','1','NA','1'],
'rob':['1','0','NA','1','1'],
'ans':['0','0','1','NA','1'],
'zsd':['1','NA','1','1','1'],
'count':['4','3','3','2','4']}
dept subd jju rob ans zsd count
0 dept1 hndf 0 1 0 1 4
1 dept2 nbf 1 0 0 NA 3
2 dept3 asx 1 NA 1 1 3
3 dept4 qwe NA 1 NA 1 2
4 dept5 def 1 1 1 1 4
df = df[df.loc[:,'jju':'zsd'].eq("1")]
。
预期结果:
0 dept3 asx 1 NA 1 1 3
1 dept4 qwe NA 1 NA 1 2
2 dept5 def 1 1 1 1 4
how to check if the selected columns in a dataframe return True and how to compare the count of True values with the column name count that is handling the count number of cells that are NOT NULL.
WHERE these selected columns can have a False as value.
for this i used the below statement but i do not know how to compare it with the column count
df=pd.DataFrame({'dept':['dept1','dept2','dept3','dept4','dept5'],
'subd':['hndf','nbf','asx','qwe','def'],
'jju':['0','1','1','NA','1'],
'rob':['1','0','NA','1','1'],
'ans':['0','0','1','NA','1'],
'zsd':['1','NA','1','1','1'],
'count':['4','3','3','2','4']}
dept subd jju rob ans zsd count
0 dept1 hndf 0 1 0 1 4
1 dept2 nbf 1 0 0 NA 3
2 dept3 asx 1 NA 1 1 3
3 dept4 qwe NA 1 NA 1 2
4 dept5 def 1 1 1 1 4
df = df[df.loc[:,'jju':'zsd'].eq("1")]
how to continue from this ???
expected result :
0 dept3 asx 1 NA 1 1 3
1 dept4 qwe NA 1 NA 1 2
2 dept5 def 1 1 1 1 4
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以用NAN值替换
“ Na”
,然后在轴上SUM
,然后使用eq
与Count> Count
进行比较。列值以创建布尔掩码。然后使用掩码过滤df
:输出:
You can replace
"NA"
by NaN values, thensum
on axis, then useeq
to compare tocount
column value to create a boolean mask. Then use the mask to filterdf
:Output:
这可以完成工作,
输出 -
This does the job,
Output -