【发布时间】:2019-05-14 04:53:00
【问题描述】:
我有一个包含三列 A、B 和 C 的数据框。我必须创建名为“Differential_value”的第四列。我必须使用某些条件为第四列赋值。 条件如下:
1) 第一个条件:如果 A、B 或 C 三列中的任何一列的值为 0,则“Differential_value”列中的 I 为 0。
2) 否则,“Differential_value”赋值应为:
(max(A,B,C) - min(A,B,C))/min(A,B,C)
Below is my sample data:
A B C
10 7 0
10 8 12
9 8 11
10 11 12
13 5 0
0 3 10
12 8 11
12 9 7
11 10 9
10 11 9
以下是我尝试过的代码:
df['differential_value'] = np.where((df['A']==0)|(df['B']==0)|(df['C']== 0),0),(np.where((df[['A','B','C']].max() - df[['A','B','C']].min())/df[['A','B','C']].min()))
ValueError: x 和 y 要么都给出,要么都不给出
【问题讨论】:
-
下面是我的尝试:df['differential_value'] = np.where((df['A']==0)|(df['B']==0)|(df ['C']== 0),0),(np.where((df[['A','B','C']].max() - df[['A','B' ,'C']].min())/df[['A','B','C']].min())) ValueError: x 和 y 都应该给出或都不给出