【问题标题】:Pandas reduce number of categorical variables in value_counts() tabulationPandas 减少了 value_counts() 列表中的分类变量的数量
【发布时间】:2016-08-23 10:55:00
【问题描述】:

熊猫新手我想执行类似于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 代码相比,这似乎很笨拙。这里的正确方法是什么?

【问题讨论】:

标签: python pandas categorical-data binning


【解决方案1】:

您可以将value_countsnumpy.where 一起使用,其中条件与isin 一起使用。

如果您的变量是对象类型,请参见下文。如果您的变量属于类别类型,则向下跳至底部。

df = pd.DataFrame({'Color':'Red Red Blue Red Violet Blue'.split(), 
                   'Value':[11,150,50,30,10,40]})
print (df)
    Color  Value
0     Red     11
1     Red    150
2    Blue     50
3     Red     30
4  Violet     10
5    Blue     40

a = df.Color.value_counts()
print (a)
Red       3
Blue      2
Violet    1
Name: Color, dtype: int64

#get top 2 values of index
vals = a[:2].index
print (vals)
Index(['Red', 'Blue'], dtype='object')

df['new'] = np.where(df.Color.isin(vals), 0,1)
print (df)
    Color  Value  new
0     Red     11    0
1     Red    150    0
2    Blue     50    0
3     Red     30    0
4  Violet     10    1
5    Blue     40    0

或者如果需要替换所有非最高值使用where:

df['new1'] = df.Color.where(df.Color.isin(vals), 'other')
print (df)
    Color  Value   new1
0     Red     11    Red
1     Red    150    Red
2    Blue     50   Blue
3     Red     30    Red
4  Violet     10  other
5    Blue     40   Blue

对于类别类型:

df = pd.DataFrame({'Color':'Red Red Blue Red Violet Blue'.split(), 
                   'Value':[11,150,50,30,10,40]})
df.Color = df.Color.astype('category')

a= df.Color.value_counts()[:2].index
print(a)
CategoricalIndex(['Red', 'Blue'], 
                categories=['Blue', 'Red', 'Violet'], 
                ordered=False, dtype='category')

请注意,紫罗兰仍然是一个类别。所以我们需要.remove_unused_categories()

vals = df.Color.value_counts()[:2].index.remove_unused_categories()
CategoricalIndex(['Red', 'Blue'], 
                 categories=['Blue', 'Red'], 
                 ordered=False, dtype='category')

如 cmets 中所述,设置新变量时会发生 ValueError。解决方法是改变类型。

df['new1'] = df.Color.astype('object').where(df.Color.isin(vals), 'other')
df['new1'] = df['new1'].astype('category')

【讨论】:

  • 我收到此错误ValueError: Cannot setitem on a Categorical with a new category, set the categories first。这是因为我的变量已经是分类的。我的解决方法是像这样添加.astype('object') df.Color.astype('object').where(df.Color.isin(vals), 'other')
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-23
  • 2014-06-20
  • 2011-11-04
  • 2021-03-19
  • 1970-01-01
  • 2016-10-11
相关资源
最近更新 更多