0
新的号码大熊猫我要(以分类变量的分级,以减少他们的水平)执行类似于Reduce number of levels for large categorical variables东西 下面的代码工作中的R熊猫降低分类变量
DTsetlvls <- function(x, newl)
setattr(x, "levels", c(setdiff(levels(x), newl), rep("other", length(newl))))
我的数据框罚款:
df = pd.DataFrame({'Color': 'Red Red Blue'.split(),
'Value': [100, 150, 50]})
df['Counts'] = df.groupby('Color')['Value'].transform('count')
print (df)
Color Value Counts
0 Red 100 2
1 Red 150 2
2 Blue 50 1
我手动创建一个聚合列,然后基于此,标记较不频繁的组,例如“蓝色”作为单个“其他”组。 但与简洁的R代码相比,这看起来很笨拙。这里的正确方法是什么?
可能[如何将“剩余的”结果分组到上N以外的结果复制到“O”中thers“with pandas](http://stackoverflow.com/questions/19835746/how-to-group-remaining-results-beyond-top-n-into-others-with-pandas) –