首页 > 解决方案 > 如何在 python pandas 的整数列上使用 .map

问题描述

我正在尝试获取一个整数列并将离散值映射到另一列。基本上,如果标记了信用等级,则 1、2、3、antoher 列将它们映射到no credit state或。然后用 填充空值。我试过但是,我不断收到此错误:no hitthin filesvaild

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-129-926e6625f2b6> in <module>
      1 #train.dtypes
----> 2 df['discrete_52278'] = df.apply(lambda row: discrete_credit(row, 'credit_52278'), axis = 1)

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py in apply(self, func, axis, broadcast, raw, reduce, result_type, args, **kwds)
   6012                          args=args,
   6013                          kwds=kwds)
-> 6014         return op.get_result()
   6015 
   6016     def applymap(self, func):

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\apply.py in get_result(self)
    140             return self.apply_raw()
    141 
--> 142         return self.apply_standard()
    143 
    144     def apply_empty_result(self):

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\apply.py in apply_standard(self)
    246 
    247         # compute the result using the series generator
--> 248         self.apply_series_generator()
    249 
    250         # wrap results

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\apply.py in apply_series_generator(self)
    275             try:
    276                 for i, v in enumerate(series_gen):
--> 277                     results[i] = self.f(v)
    278                     keys.append(v.name)
    279             except Exception as e:

<ipython-input-129-926e6625f2b6> in <lambda>(row)
      1 #train.dtypes
----> 2 df['discrete_52278'] = df.apply(lambda row: discrete_credit(row, 'credit_52278'), axis = 1)

<ipython-input-126-462888d46184> in discrete_credit(row, variable)
      6 
      7     """
----> 8     score = row[variable].map({1:'no_credit_state', 2:'thin_file', 3:"no_hit"})
      9     score = row[score].fillna('valid')
     10     score = pd.Categorical(row[score], ['valid', 'no_credit_state','thin_file', 'no_hit'])

AttributeError: ("'numpy.int64' object has no attribute 'map'", 'occurred at index 0')

这是一个引发相同错误的代码示例:

import pandas as pd
credit = {'credit_52278':[1,2,3,500,550,600,650,700,750,800,900]      
            }
df = pd.DataFrame(credit)


def discrete_credit(row, variable):
    """

    allows thin files, no hits and no credit scores to float which will then allow the rest of the credit score to be fit \
    with a spline

    """
    score = row[variable].map({1:'no_credit_state', 2:'thin_file', 3:"no_hit"})
    score = row[score].fillna('valid')
    score = pd.Categorical(row[score], ['valid', 'no_credit_state','thin_file', 'no_hit'])
    return score

df['discrete_52278'] = df.apply(lambda row: discrete_credit(row, 'credit_52278'), axis = 1)

标签: pythonpandasfunction

解决方案


map是一个 Series 方法,但您试图在标量(浮点)值上使用它。

您可以简单地执行以下操作:

df['discrete_52278'] = (
    df['credit_52278']
    .map({
        1: 'no_credit_state', 
        2: 'thin_file', 
        3: 'no_hit'
    })
    .fillna('valid')
    .astype('category')
)

推荐阅读