Posted in Python onNovember 15, 2018
如下所示:
def Family_feature(df): df['Fam_Size'] =df['SibSp']+df['Parch'] df['Fam_Size'].loc[df['Fam_Size'] == 0] = 1 df['Fam_Size'].loc[(df['Fam_Size'] > 1) & (df['Fam_Size'] <= 3)] = 2 # df['Fam_Size'].loc[df['Fam_Size'] == 2] = 2 # df['Fam_Size'].loc[df['Fam_Size'] == 3] = 2 df['Fam_Size'].loc[df['Fam_Size'] > 3] = 3 return df
df['Fam_Size'].loc[(df['Fam_Size'] > 1) & (df['Fam_Size'] <= 3)] = 2 只要中间加一个 & 符号 , 然后把两边的condition括号起来就行。
以上这篇对pandas里的loc并列条件索引的实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。
对pandas里的loc并列条件索引的实例讲解
- Author -
akon_wang_hkbu声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@