如何在python中的整数列上使用.map

如何在python中的整数列上使用.map,python,pandas,function,Python,Pandas,Function,我试图获取一个整数列,并将离散值映射到另一列。基本上,如果标记了信用等级1、2、3,则另一列会将其映射到无信用状态、无命中或精简文件。然后用vaild填充空值。我试过了,但是我一直遇到这样的错误: --------------------------------------------------------------------------- AttributeError Traceback (most recent call last)

我试图获取一个整数列,并将离散值映射到另一列。基本上,如果标记了信用等级1、2、3,则另一列会将其映射到
无信用状态
无命中
精简文件
。然后用
vaild
填充空值。我试过了,但是我一直遇到这样的错误:

---------------------------------------------------------------------------
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')

map
是一种级数方法,但您试图在标量(浮点)值上使用它

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

df['discrete_52278']=(
df['credit_52278']
.地图({
1:“无信用状态”,
2:'精简文件',
3:“没有命中”
})
.fillna('有效')
.astype('类别')
)

我运行它,它不会抛出任何错误,但当我运行df['discrete_52278']时,数据类型就不存在了。
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)