我想过滤名称,其中a列中的所有值均为nan
这是我尝试过的
full.groupby('name')['opp'].isna().any(1)
但是这将返回错误消息:
AttributeError: Cannot access callable attribute 'isna' of 'SeriesGroupBy' objects, try using the 'apply' method
这是我的数据示例
name opp
f nan
f nan
g f
g nan
g nan
g nan
k nan
所需的输出是
name opp
f nan
f nan
k nan
答案 0 :(得分:1)
使用GroupBy.transform
和GroupBy.all
来测试每个组是否所有True
(所有NaN
值)并按boolean indexing
进行过滤:
df = full[full['opp'].isna().groupby(full['name']).transform('all')]
替代方法是获取所有包含至少一个非缺失值的name
,并按Series.isin
过滤原始名称列:
df = full[~full['name'].isin(full.loc[full['opp'].notna(), 'name'])]
print (df)
name opp
0 f NaN
1 f NaN
6 k NaN
答案 1 :(得分:1)