0

I'm trying to take an integer column and map discrete values to another column. Basically, if a credit tier is marked, 1, 2, 3, antoher column maps those to no credit state, no hit or thin files. Then fill the null values with vaild. I tried However, I keep getting this error:

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

Here is a code example that is throwing the same error:

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 Answer 1

1

map is a Series method, but you are trying to use it on a scalar (float) value.

You could simply do something like:

df['discrete_52278'] = (
    df['credit_52278']
    .map({
        1: 'no_credit_state', 
        2: 'thin_file', 
        3: 'no_hit'
    })
    .fillna('valid')
    .astype('category')
)
Sign up to request clarification or add additional context in comments.

1 Comment

I run this, and it throws no errors, but when I do df['discrete_52278'].dtypes, it's not there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.