Python 如何用一列减去另一列中的排除标志
我在熊猫中有以下数据帧Python 如何用一列减去另一列中的排除标志,python,pandas,Python,Pandas,我在熊猫中有以下数据帧 code time_diff diff_flag quantity 123 0 zero 0.45 124 5 less than 6 0.80 125 8 no issue 0.78 126 18 no issue 2.78 127
code time_diff diff_flag quantity
123 0 zero 0.45
124 5 less than 6 0.80
125 8 no issue 0.78
126 18 no issue 2.78
127 28 no issue 4.78
我想从每一行的数量中减去6,除了diff_flag
0和小于6。我想要的数据帧是
code time_diff diff_flag quantity new_diff
123 0 zero 0.45 Data Error
124 5 less than 6 0.80 Data Error
125 8 no issue 0.78 2
126 18 no issue 2.78 12
127 28 no issue 4.78 22
我如何在熊猫身上做到这一点?使用:
或:
为什么不:
df['new_diff']=(df['time_diff']-6).clip(lower='Data Error')
m1 = df['time_diff'] == 0
m2 = df['time_diff'] < 6
df['new_diff'] = np.where(m1 | m2, 'Data Error', df['time_diff'] - 6)
m = df['diff_flag'] == 'no issue'
df['new_diff'] = np.where(m, df['time_diff'] - 6, 'Data Error')
print (df)
code time_diff diff_flag quantity new_diff
0 123 0 zero 0.45 Data Error
1 124 5 less than 6 0.80 Data Error
2 125 8 no issue 0.78 2
3 126 18 no issue 2.78 12
4 127 28 no issue 4.78 22
df['new_diff']=(df['time_diff']-6).clip(lower='Data Error')