1 Ошибка 'int' object is not subscriptable

Нужно написать функцию заменяющую нулевые значения в одном столбце, средними значениями (по категориям отмеченным в другом столбце). Написал такой код:

a = int(df[df['family_status_id'] == 0]['dob_years'].mean())
b = int(df[df['family_status_id'] == 1]['dob_years'].mean())
c = int(df[df['family_status_id'] == 2]['dob_years'].mean())
d = int(df[df['family_status_id'] == 3]['dob_years'].mean())
e = int(df[df['family_status_id'] == 4]['dob_years'].mean())

def age_raplace(row):
    age = row['dob_years']
    family = row['family_status_id']
    if age == 0: 
        if family == 0:
            return a
        if family == 1:
            return b
        if family == 2:
            return c
        if family == 3:
            return d
        if family == 3:
            return e
 
df['dob_years'].apply(age_raplace)  
print(df[df['dob_years'] == 0])

Получаю ошибку:

TypeError                                 Traceback (most recent call last)
<ipython-input-84-2e0ec2934169> in <module>
     21 
     22 #df['dob_years'] = df['dob_years'].apply(age_raplace)
---> 23 df['dob_years'].apply(age_raplace)
     24 print(df[df['dob_years'] == 0])
     25 

/opt/conda/lib/python3.7/site-packages/pandas/core/series.py in apply(self, func, convert_dtype, args, **kwds)
   4040             else:
   4041                 values = self.astype(object).values
-> 4042                 mapped = lib.map_infer(values, f, convert=convert_dtype)
   4043 
   4044         if len(mapped) and isinstance(mapped[0], Series):

pandas/_libs/lib.pyx in pandas._libs.lib.map_infer()

<ipython-input-84-2e0ec2934169> in age_raplace(row)
      6 
      7 def age_raplace(row):
----> 8     age = row['dob_years']
      9     family = row['family_status_id']
     10     if age == 0:

TypeError: 'int' object is not subscriptable

Подскажите в чем проблема?


Ответы (0 шт):