简体   繁体   English

使用熊猫替换模块用数值替换数据中的分类值时出错

[英]Error during replacement of categorical values in data with numerical values using Pandas replace module

AttributeError: module 're' has no attribute '_pattern_type' AttributeError:模块“ re”没有属性“ _pattern_type”

I am getting the above error while trying to replace categorical data in a dataset with numerical values using Pandas replace module. 尝试使用Pandas替换模块用数值替换数据集中的分类数据时出现上述错误。 My code is as follows. 我的代码如下。 housng_dataframe is a dataframe created on a housing dataset using Pandas. housng_dataframe是使用Pandas在住房数据集上创建的数据框。

ordinal_mapping_dict = {"LotShape": {"Reg": 3,"IR1": 2,"IR2": 1,"IR3": 0}, 
                        "ExterQual":{"Ex": 4,"Gd": 3,"TA": 2,"Fa": 1,"Po": 0},
                        "HeatingQC":{"Ex": 4,"Gd": 3,"TA": 2,"Fa": 1,"Po": 0},
                        "KitchenQual":{"Ex": 4,"Gd": 3,"TA": 2,"Fa": 1,"Po": 0},
                        "Functional":{"Typ":7,"Min1":6,"Min2":5,"Mod":4,"Maj1":3,"Maj2":2,"Sev":1,"Sal":0},
                        "PavedDrive":{"Y": 2,"P": 1,"N": 0 }}

housing_dataframe = housing_dataframe.replace(ordinal_mapping_dict)

Output: 输出:

AttributeError                            Traceback (most recent call last)
<ipython-input-106-d0830be43fa5> in <module>()
      6                         "PavedDrive":{"Y": 2,"P": 1,"N": 0 }}
      7 
----> 8 housing_dataframe = housing_dataframe.replace(ordinal_mapping_dict)

c:\users\diro\appdata\local\programs\python\python37\lib\site-packages\pandas\core\generic.py in replace(self, to_replace, value, inplace, limit, regex, method, axis)
   4529 
   4530             return self.replace(to_replace, value, inplace=inplace,
-> 4531                                 limit=limit, regex=regex)
   4532         else:
   4533 

c:\users\diro\appdata\local\programs\python\python37\lib\site-packages\pandas\core\generic.py in replace(self, to_replace, value, inplace, limit, regex, method, axis)
   4548                                                     value=value[c],
   4549                                                     inplace=False,
-> 4550                                                     regex=regex)
   4551                     return None if inplace else res
   4552 

c:\users\diro\appdata\local\programs\python\python37\lib\site-packages\pandas\core\generic.py in replace(self, to_replace, value, inplace, limit, regex, method, axis)
   4578                                                        dest_list=value,
   4579                                                        inplace=inplace,
-> 4580                                                        regex=regex)
   4581 
   4582                 else:  # [NA, ''] -> 0

c:\users\diro\appdata\local\programs\python\python37\lib\site-packages\pandas\core\internals.py in replace_list(self, src_list, dest_list, inplace, regex, mgr)
   3500                         convert = i == src_len
   3501                         result = b.replace(s, d, inplace=inplace, regex=regex,
-> 3502                                            mgr=mgr, convert=convert)
   3503                         new_rb = _extend_blocks(result, new_rb)
   3504                     else:

c:\users\diro\appdata\local\programs\python\python37\lib\site-packages\pandas\core\internals.py in replace(self, to_replace, value, inplace, filter, regex, convert, mgr)
   2206         blocks = [self]
   2207 
-> 2208         if not either_list and is_re(to_replace):
   2209             return self._replace_single(to_replace, value, inplace=inplace,
   2210                                         filter=filter, regex=True,

c:\users\diro\appdata\local\programs\python\python37\lib\site-packages\pandas\core\dtypes\inference.py in is_re(obj)
    200     """
    201 
--> 202     return isinstance(obj, re._pattern_type)
    203 
    204 

AttributeError: module 're' has no attribute '_pattern_type'

This was a bug in pandas replace method, it's since been fixed (the fix will be in 0.23.0). 这是熊猫替换方法中的一个错误已得到修复 (此修复将在0.23.0中进行)。

Python 3.7 removed the private function _pattern_type Python 3.7删除了私有函数_pattern_type
(ref: https://github.com/spulec/moto/issues/1546). (参考: https : //github.com/spulec/moto/issues/1546)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM