这篇文章主要介绍如何实现pandas里的loc并列条件索引,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
如下所示:
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并列条件索引”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注天达云行业资讯频道!