[英]Python User Defined Function using IF statements not working
我有以下數據框
Index education marital-status occupation gender target
0 bachelors never-married adm-clerical male 0
1 bachelors spouse exec-managerial male 0
2 hs-grad divorced handlers-cleaners male 0
3 11th spouse handlers-cleaners male 0
4 bachelors spouse prof-specialty female 0
5 masters spouse exec-managerial female 0
6 other other other-service female 0
7 hs-grad spouse exec-managerial male 1
8 masters never-married prof-specialty female 1
9 bachelors spouse exec-managerial male 1
有人可以向我解釋以下原因為何不起作用-我認為應該根據我已閱讀的內容和所看到的內容進行應用。
def new_features(education, gender, target):
if [((education == 'bachelors') & (gender == 'male') & (target == 1))]:
result = 'educated_male_convert'
elif [((education == 'bachelors') & (gender == 'female') & (target == 1))]:
result = 'educated_female_convert'
else:
result = 'educated_not_determined'
return result
df['new_col'] = df.apply(lambda row: new_features(row['education'], row['gender'], row['target']), axis=1)
它只是返回: educated_male_convert
我遵循了許多教程,並閱讀了其他主題,並將相同的代碼應用於自己的數據集-不知道我缺少什么。
任何幫助,將不勝感激
問題是您將if
條件放在方括號中。 因此,代碼不是在測試表達式if False: ...
,而是在測試if [False]: ...
並且由於任何非空列表的評估結果為True
,因此[False]
評估結果為True
,並且代碼轉到錯誤的分支。
這也是這樣做的另一種方式:
df['new_col'] = df.apply(lambda row: 'educated_male_convert' if row['education'] == 'bachelors' and row['gender'] == 'male' and row['target'] == 1
else ('educated_female_convert' if row['education'] == 'bachelors' and row['gender'] == 'female' and row['target'] == 1
else ('educated_not_determined')), axis=1)
df
這是一個np.select
解決方案:
c1=df.education=='bachelors'
c2=df.gender=='male'
c3=df.target.astype(bool)
df['new_col']=np.select([c1&c2&c3,c1&~c2&c3],['educated_male_convert',
'educated_female_convert'],'educated_not_determined')
print(df)
education marital-status occupation gender target \
Index
0 bachelors never-married adm-clerical male 0
1 bachelors spouse exec-managerial male 0
2 hs-grad divorced handlers-cleaners male 0
3 11th spouse handlers-cleaners male 0
4 bachelors spouse prof-specialty female 0
5 masters spouse exec-managerial female 0
6 other other other-service female 0
7 hs-grad spouse exec-managerial male 1
8 masters never-married prof-specialty female 1
9 bachelors spouse exec-managerial male 1
new_col
Index
0 educated_not_determined
1 educated_not_determined
2 educated_not_determined
3 educated_not_determined
4 educated_not_determined
5 educated_not_determined
6 educated_not_determined
7 educated_not_determined
8 educated_not_determined
9 educated_male_convert
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.