手动分配类别的最佳方法

2024-04-27 08:15:58 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有一个数据帧,其值如下:


Food
----
Turkey
Tomato
Rice
Chicken
Lettuce

我想添加一个类别,使其看起来像:

Food        Category
----        ----
Turkey      Meat
Tomato      Vegetable
Rice        Grain
Chicken     Meat
Lettuce     Vegetable

但实际上我有大约100个不同的值,我想把它们分为大约10组,我想手工操作。你知道吗

我一直在尝试直接编写脚本,而不是链接数据库或电子表格。到目前为止,我一直在尝试的是打印下面的错误代码,但也不知道是否有更好的方法来实现这一点?你知道吗

当前代码:

df.loc[df.Food.any(
    [
    'Turkey'
    ,'Chicken'
]
)
         , 'Category'] = 'Meat' 

df.loc[df.Food.any(
    [
    'Tomato'
    ,'Lettuce'
]
)
         , 'Category'] = 'Vegetable' 

错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-49-41349bcd38a0> in <module>
     41     ]
     42 )
---> 43          , 'Category'] = 'Meat' 

~\AppData\Local\Continuum\miniconda3\lib\site-packages\pandas\core\generic.py in logical_func(self, axis, bool_only, skipna, level, **kwargs)
  11721             skipna=skipna,
  11722             numeric_only=bool_only,
> 11723             filter_type="bool",
  11724         )
  11725 

~\AppData\Local\Continuum\miniconda3\lib\site-packages\pandas\core\series.py in _reduce(self, op, name, axis, skipna, numeric_only, filter_type, **kwds)
   4061 
   4062         if axis is not None:
-> 4063             self._get_axis_number(axis)
   4064 
   4065         if isinstance(delegate, Categorical):

~\AppData\Local\Continuum\miniconda3\lib\site-packages\pandas\core\generic.py in _get_axis_number(cls, axis)
    400     @classmethod
    401     def _get_axis_number(cls, axis):
--> 402         axis = cls._AXIS_ALIASES.get(axis, axis)
    403         if is_integer(axis):
    404             if axis in cls._AXIS_NAMES:

TypeError: unhashable type: 'list'

Tags: inonlydfgetiffoodclscategory
1条回答
网友
1楼 · 发布于 2024-04-27 08:15:58

我建议将映射值存储在字典中,以类别作为键,并将对应于该类别的选项列表作为值,如下所示:

mapping = {'Meat': ['Turkey','Chicken'], 'Vegetable': ['Tomato','Lettuce'], 'Grain': ['Rice']}

然后可以使用^{}

df['Category'] = df['Food'].map({i: k for k, v in mapping.items() for i in v})

收益率:

      Food   Category
0   Turkey       Meat
1   Tomato  Vegetable
2     Rice      Grain
3  Chicken       Meat
4  Lettuce  Vegetable

相关问题 更多 >