对pandas里的loc并列条件索引的实例讲解
程序员文章站
2023-11-15 23:25:16
如下所示:
def family_feature(df):
df['fam_size'] =df['sibsp']+df['parch']
df['fam...
如下所示:
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并列条件索引的实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。