Как использовать .map для целочисленного столбца в пандах Python - PullRequest
0 голосов
/ 09 мая 2019

Я пытаюсь взять целочисленный столбец и отобразить дискретные значения в другой столбец. В основном, если кредитный уровень отмечен, 1, 2, 3, другой столбец отображает их на no credit state, no hit или thin files. Затем заполните нулевые значения 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')

Вот пример кода, который выдает ту же ошибку:

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)

1 Ответ

1 голос
/ 09 мая 2019

map - это метод Series, но вы пытаетесь использовать его для скалярного (плавающего) значения.

Вы можете просто сделать что-то вроде:

df['discrete_52278'] = (
    df['credit_52278']
    .map({
        1: 'no_credit_state', 
        2: 'thin_file', 
        3: 'no_hit'
    })
    .fillna('valid')
    .astype('category')
)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...