当前位置:主页 > 软件编程 > Python代码 >

对pandas里的loc并列条件索引的实例讲解

时间:2022-02-17 09:29:30 | 栏目:Python代码 | 点击:

如下所示:

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括号起来就行。

您可能感兴趣的文章:

相关文章