0

I made some function like this: data['Age'] = data[['Age',School]].apply(age_implementation, axis = 1) by doing so I want to full NaN values in "Age" column based on School of child and this is the definitione of my "age_implementation" function. Nevertheless when I try to apply function using code above nothing changes, how can I apply this function ?

Function:

def age_implementation(cols):
    Age = cols[0]
    School= cols[1]

    if pd.isnull(Age):
        if School== 1:
            return 10
        elif School== 2:
            return 15
        elif School== 3:
            return 20
        else:
            return Age

Data:

data = pd.DataFrame("School":{1,2,3,1,2,2}, "Age":{NaN, NaN, 20, NaN, NaN, 15})
dingaro
  • 2,156
  • 9
  • 29
  • 1
    1. We don't have your data 2. we don't have your code (`age_implementation`) 3. we can't see the output or error messages. Can you please fix these issues so someone can answer your question? – cs95 Dec 13 '19 at 19:20
  • could you now help me ? – dingaro Dec 13 '19 at 19:32
  • Yeah, you are assigning Age and School incorrectly. Hint: What is the type of `cols` inside the function? – cs95 Dec 13 '19 at 19:42

2 Answers2

1

With how your doing it, i would implement it this way:

def age_implementation(cols):
    Age = cols.Age
    School= cols.School

    if pd.isnull(Age):
        if School== 1:
            return 10
        elif School== 2:
            return 15
        elif School== 3:
            return 20
    else:
         return Age

data['Age'] = data.apply(age_implementation, axis = 1).astype(int) 

output:

   School  Age
0       1   10
1       2   15
2       3   20
3       1   10
4       2   15
5       2   15

oppressionslayer
  • 6,942
  • 2
  • 7
  • 24
0

We can use Series.map, this is faster:

data['Age']=data['Age'].fillna(data['School'].map({1:10,2:15,3:20}))
print(data)

Output

   School   Age
0       1  10.0
1       2  15.0
2       3  20.0
3       1  10.0
4       2  15.0
5       2  15.0
ansev
  • 30,322
  • 5
  • 17
  • 31