我有一个数据框,我想groupby
by bq_market_id
然后检查是否有NaN
值在bq_back_price
在每组中如果是那么True
每组如果没有则False
每组。
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
我该怎么做呢?我尝试了以下方法,但没有成功。
bb.groupby('bq_market_id')['bq_back_price'].isnull().any()
我想你可以使用apply http://pandas.pydata.org/pandas-docs/stable/generated/pandas.core.groupby.GroupBy.apply.html:
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
已更改):
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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)