计算“ true”数据框中的值

发布于 2025-01-23 14:39:41 字数 1175 浏览 3 评论 0原文

如何检查数据框中的选定列返回 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静若繁花 2025-01-30 14:39:41

您可以用NAN值替换“ Na”,然后在轴上SUM,然后使用eqCount> Count进行比较。列值以创建布尔掩码。然后使用掩码过滤df

msk = df['count'].astype(int).eq(df.loc[:, 'jju':'zsd'].replace('NA', float('nan')).astype(float).eq(1).sum(axis=1))
out = df[msk]

输出:

    dept subd jju rob ans zsd count
2  dept3  asx   1  NA   1   1     3
3  dept4  qwe  NA   1  NA   1     2
4  dept5  def   1   1   1   1     4

You can replace "NA" by NaN values, then sum on axis, then use eq to compare to count column value to create a boolean mask. Then use the mask to filter df:

msk = df['count'].astype(int).eq(df.loc[:, 'jju':'zsd'].replace('NA', float('nan')).astype(float).eq(1).sum(axis=1))
out = df[msk]

Output:

    dept subd jju rob ans zsd count
2  dept3  asx   1  NA   1   1     3
3  dept4  qwe  NA   1  NA   1     2
4  dept5  def   1   1   1   1     4
能怎样 2025-01-30 14:39:41

这可以完成工作,

df.replace("NA", np.nan, inplace = True)
df.iloc[:, 2:] = df.iloc[:, 2:].apply(pd.to_numeric, errors = "coerce", axis = 1)

输出 -

deptsubdjjurobanszsdcount
0dept1hndf0.01.00.01.04.0
1dept2nbf1.00.00.0nan3.0
2dept3asx1.0nan1.01.03.0
3dept4qwenan1.0nan1.02.0
4dept5def1.01.01.01.04.0

This does the job,

df.replace("NA", np.nan, inplace = True)
df.iloc[:, 2:] = df.iloc[:, 2:].apply(pd.to_numeric, errors = "coerce", axis = 1)

Output -

deptsubdjjurobanszsdcount
0dept1hndf0.01.00.01.04.0
1dept2nbf1.00.00.0nan3.0
2dept3asx1.0nan1.01.03.0
3dept4qwenan1.0nan1.02.0
4dept5def1.01.01.01.04.0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文