应用功能熊猫??实现

问题描述 投票:0回答:2

我做了这样的函数:data['Age'] = data[['Age',School]].apply(age_implementation, axis = 1),因此我想根据孩子的学校填写“年龄”列中的NaN值,这是我的“年龄实现”函数的定义。但是,当我尝试使用上面没有任何变化的代码来应用功能时,如何应用此功能?

功能:

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 = pd.DataFrame("School":{1,2,3,1,2,2}, "Age":{NaN, NaN, 20, NaN, NaN, 15})
python pandas apply implementation
2个回答
1
投票

[您的操作方式,我将以这种方式实现:

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) 

输出:

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


0
投票

我们可以使用Series.map,速度更快:

Series.map

输出

data['Age']=data['Age'].fillna(data['School'].map({1:10,2:15,3:20}))
print(data)
© www.soinside.com 2019 - 2024. All rights reserved.