【问题标题】:Rename the less frequent categories by "OTHER" python用“其他”python重命名不太频繁的类别
【发布时间】:2019-05-07 23:04:29
【问题描述】:

在我的数据框中,我有一些包含 100 多个不同类别的分类列。我想按最常见的类别对类别进行排名。我保留前 9 个最常见的类别,而不太常见的类别会自动将它们重命名为:OTHER

例子:

这里是我的 df:

print(df)

    Employee_number                 Jobrol
0                 1        Sales Executive
1                 2     Research Scientist
2                 3  Laboratory Technician
3                 4        Sales Executive
4                 5     Research Scientist
5                 6  Laboratory Technician
6                 7        Sales Executive
7                 8     Research Scientist
8                 9  Laboratory Technician
9                10        Sales Executive
10               11     Research Scientist
11               12  Laboratory Technician
12               13        Sales Executive
13               14     Research Scientist
14               15  Laboratory Technician
15               16        Sales Executive
16               17     Research Scientist
17               18     Research Scientist
18               19                Manager
19               20        Human Resources
20               21        Sales Executive


valCount = df['Jobrol'].value_counts()

valCount

Sales Executive          7
Research Scientist       7
Laboratory Technician    5
Manager                  1
Human Resources          1

我保留前 3 个类别,然后将其余类别重命名为“OTHER”,我应该如何继续?

谢谢。

【问题讨论】:

  • 我的类别中有一些NaN值,如果我不想重命名它们我应该如何进行过滤?
  • 如果我想重命名所有分类

标签: python pandas dataframe counter categorical-data


【解决方案1】:

一线解决方案:

limit = 500
df['Jobrol'] = df['Jobrol'].map({x[0]: x[0] if x[1] > limit else 'other' for x in dict(df['Jobrol'].value_counts()).items()})

【讨论】:

    【解决方案2】:

    将您的系列转换为分类,提取计数不在前 3 的类别,添加一个新类别,例如'Other',然后替换之前计算的类别:

    df['Jobrol'] = df['Jobrol'].astype('category')
    
    others = df['Jobrol'].value_counts().index[3:]
    label = 'Other'
    
    df['Jobrol'] = df['Jobrol'].cat.add_categories([label])
    df['Jobrol'] = df['Jobrol'].replace(others, label)
    

    注意:通过df['Jobrol'].cat.rename_categories(dict.fromkeys(others, label)) 重命名类别来组合类别是诱人的,但这不起作用,因为这将意味着多个相同标记的类别,这是'不可能。


    上面的解决方案可以适应count过滤。例如,要仅包含计数为 1 的类别,您可以这样定义 others

    counts = df['Jobrol'].value_counts()
    others = counts[counts == 1].index
    

    【讨论】:

    • 我的类别中有一些 NaN 值,如果我不想重命名它们,我应该如何进行过滤?谢谢
    • 类似others = df['Jobrol'].value_counts().dropna().index[3:]
    • 如果我想重命名所有分类值
    • 赞成,因为这是最直观的阅读解决方案并且仅使用 Pandas
    【解决方案3】:

    value_countsnumpy.where 一起使用:

    need = df['Jobrol'].value_counts().index[:3]
    df['Jobrol'] = np.where(df['Jobrol'].isin(need), df['Jobrol'], 'OTHER')
    
    valCount = df['Jobrol'].value_counts()
    print (valCount)
    Research Scientist       7
    Sales Executive          7
    Laboratory Technician    5
    OTHER                    2
    Name: Jobrol, dtype: int64
    

    另一种解决方案:

    N = 3
    s = df['Jobrol'].value_counts()
    valCount = s.iloc[:N].append(pd.Series(s.iloc[N:].sum(), index=['OTHER']))
    print (valCount)
    Research Scientist       7
    Sales Executive          7
    Laboratory Technician    5
    OTHER                    2
    dtype: int64
    

    【讨论】:

    • 即使使用此解决方案,我也建议转换为分类数据。否则,此解决方案不会矢量化(pd.Series.isin 与分类是数字数据;否则与 object dtype)。
    • 我的类别中有一些NaN值,如果我不想重命名它们我应该如何进行过滤?
    • @IbD - 在应用解决方案之前是否需要删除 NaN 行?
    • 所以others = df['Jobrol'].value_counts().dropna().index[3:] 来自另一个答案的评论的解决方案是错误的,因为与others = df['Jobrol'].value_counts().index[3:] 一样工作。还是缺少什​​么?
    • @IbD - 没有测试,抱歉。需要s = df['Jobrol'].value_counts()df['Jobrol'] = np.where(df['Jobrol'].isin(s.index[s < 2]), 'OTHER', df['Jobrol'])
    猜你喜欢
    • 2021-01-19
    • 2018-04-22
    • 1970-01-01
    • 1970-01-01
    • 2012-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多