我的原始数据就像这样,只有第一行:categories id products
0 A 1 a
1 B 1 a
2 C 1 a
3 A 1 b
4 B 1 b
5 A 2 c
6 B 2 c
我用以下代码聚合了它:df2 = df.groupby('id').products.nunique().reset_index().merge(
pd.crosstab(df.id, df.categories).reset_index()
然后,我在DF中添加了n个异常值:id products A B C
0 1 2 2 2 1
1 2 1 1 1 0
2 3 50 1 1 30
现在,我正在尝试删除我的新df中的异常值:#remove outliners
del df2['id']
df2 = df2.loc[df2['products']<=20,[str(i) for i in df2.columns]]
我得到的是:products A B C
0 2 NaN NaN NaN
1 1 NaN NaN NaN
它移除异常值,但为什么我现在只在类别列中获得NaNs?