在dataframe多列元素操作(Pandas)

时间:2013-09-20 21:04:49

标签: python pandas

给定一个DataFrame df,我想做类似的事情:

if df['one'] >= df['two']:
    df['three'] = df['four']
    df['five'] = df['two'] / df['four']

1 个答案:

答案 0 :(得分:3)

.loc属性与布尔索引一起使用。

condition = df.one >= df.two
df.loc[condition, 'three'] = df['four']
df.loc[condition, 'five'] = df['two'] / df['four']