Pandas 分组依据,然后根据条件筛选组
上校 首先使用批处理对组进行分组,然后使用结果列上的条件筛选组 代码Pandas 分组依据,然后根据条件筛选组,pandas,group-by,Pandas,Group By,上校 首先使用批处理对组进行分组,然后使用结果列上的条件筛选组 代码 Batch result 输出 for key, value in df.groupby(df.batch.str[:7]): print('\n****',key,'****', '\n\n', value['result'].isnull()) 也试过 **** qwe6404 **** 19099 False 19126 False Name: Result, dtype: bool
Batch result
输出
for key, value in df.groupby(df.batch.str[:7]):
print('\n****',key,'****', '\n\n', value['result'].isnull())
也试过
**** qwe6404 ****
19099 False
19126 False
Name: Result, dtype: bool
**** qwe8135 ****
18863 True
18866 False
Name: Result, dtype: bool
如何筛选同时具有True
的组?使用:
为了提高性能,请通过以下方式使用和筛选:
.filter(lambda g: ( g.result.isnull().sum() == 2 ))
df = df.groupby(df.batch.str[:7]).filter(lambda g: g.result.isnull().all())
df[df.result.isnull().groupby(df.batch.str[:7]).transform('all')]