如何有条件地更新Pandas中的DataFrame列 [英] How to conditionally update DataFrame column in Pandas

查看:127
本文介绍了如何有条件地更新Pandas中的DataFrame列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用此DataFrame,当line_race等于零时,如何有条件地将rating设置为0?

With this DataFrame, how can I conditionally set rating to 0 when line_race is equal to zero?

    line_track  line_race  rating foreign
 25        MTH         10     84    False
 26        MTH          6     88    False
 27        TAM          5     87    False
 28         GP          2     86    False
 29         GP          7     59    False
 30        LCH          0    103     True
 31        LEO          0    125     True
 32        YOR          0    126     True
 33        ASC          0    124     True

换句话说,在DataFrame上说ColumnA = x然后ColumnB = y否则ColumnB = ColumnB的正确方法是什么

In other words, what is the proper way on a DataFrame to say if ColumnA = x then ColumnB = y else ColumnB = ColumnB

推荐答案

df.loc[df['line_race'] == 0, 'rating'] = 0

这篇关于如何有条件地更新Pandas中的DataFrame列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆