检查组是否有 NaN 值 [英] Check if the group has NaN values

查看:62
本文介绍了检查组是否有 NaN 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,我想通过 bq_market_id groupby 然后检查 bq_back_price 中是否有任何 NaN 在每组中如果是然后 True 每组如果没有然后 False 每组.

I have a dataframe and I would like to groupby by bq_market_id and then check if there is any NaN values in bq_back_price in each group if yes then True per group if no then False per group.

bq_selection_id bq_balance  bq_market_id  bq_back_price
0         45094462     185.04       7278437           1.97
1         45094462     185.04       7278437           1.97
2         45094463     185.04       7278437           3.05
3         45094463     185.04       7278437           3.05
4         45094464     185.04       7278437           5.80
5         45094464     185.04       7278437           5.80
6         45094466     185.04       7278437         200.00
7         45094466     185.04       7278437         200.00
8         45094465     185.04       7278437            NaN
9         45094465     185.04       7278437            NaN

我该怎么做?我尝试了以下方法,但没有奏效.

How do i do this? I tried the following, but it did not work.

bb.groupby('bq_market_id')['bq_back_price'].isnull().any()

推荐答案

我认为你可以使用 apply:

I think you can use apply:

print bb.groupby('bq_market_id')['bq_back_price'].apply(lambda x: x.isnull().any())
bq_market_id
7278437    True
Name: bq_back_price, dtype: bool

示例(bq_market_id 列中的某些值已更改):

Sample (some values in column bq_market_id are changed):

print bb
   bq_selection_id  bq_balance  bq_market_id  bq_back_price
0         45094462      185.04             1           1.97
1         45094462      185.04             1           1.97
2         45094463      185.04             1           3.05
3         45094463      185.04       7278437           3.05
4         45094464      185.04       7278437           5.80
5         45094464      185.04       7278437           5.80
6         45094466      185.04       7278437         200.00
7         45094466      185.04       7278437         200.00
8         45094465      185.04       7278437            NaN
9         45094465      185.04       7278437            NaN

print bb.groupby('bq_market_id')['bq_back_price'].apply(lambda x: x.isnull().any())
bq_market_id
1          False
7278437     True
Name: bq_back_price, dtype: bool

这篇关于检查组是否有 NaN 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆