-
长风秋雁
用 np.wheredf['total'] = np.where(df['multiply'] == 'Y', df['total'] * df['tax'], df['total'])print(df)multiply total tax Y 50.0 0.1 Y 125.0 0.5 N 300.0 0.5
-
MM们
在这里,我们哥们df.loc[df.multiply == 'Y', 'total'] = df.loc[df.multiply == 'Y', 'total'] * df.loc[df.multiply == 'Y', 'tax']
-
慕容森
可以那样做df.loc[df['multiply'] == 'Y', 'total'] = df.loc[df['multiply'] == 'Y', 'total'] * df.loc[df['multiply'] == 'Y', 'tax']使用pandas where和assign的另一种方法是:df.where(df['multiply'] == 'N', df.assign(total = df['total'] * df['tax']))输出: multiply total tax0 Y 50 0.11 Y 125 0.52 N 300 0.5