1

I have a dataframe with three columns A, B and C. I have to create fourth column with the name "Differential_value". I have to assign values to this fourth column using some condition. The condition is as follows:

1) First condition: if any of the three columns A,B or C has 0 value, then the I have 0 in "Differential_value" column.

2) Otherwise,"Differential_value" assigned value should be: (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

Below is my code that I have tried:

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: either both or neither of x and y should be given

1
  • Below is my try : 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: either both or neither of x and y should be given Commented May 14, 2019 at 4:59

2 Answers 2

3

Use np.where with following logic. Also, if you have a set of columns on which you want to apply then:

cols= ['A','B','C']
df['Differential_value'] = (np.where(df[cols].eq(0).any(1), 0,
                                     (df[cols].max(1) - df[cols].min(1))/df[cols].min(1)))

Or:

df['Differential_value'] = (((df[cols].max(1) - df[cols].min(1))/df[cols].min(1))
                              .replace(np.inf, 0))

print(df)
    A   B   C  Differential_value
0  10   7   0            0.000000
1  10   8  12            0.500000
2   9   8  11            0.375000
3  10  11  12            0.200000
4  13   5   0            0.000000
5   0   3  10            0.000000
6  12   8  11            0.500000
7  12   9   7            0.714286
8  11  10   9            0.222222
9  10  11   9            0.222222
Sign up to request clarification or add additional context in comments.

2 Comments

Solution is good, only add subset like cols = ['A','B','C'] and np.where(df[cols].eq(0).any(1), 0, (df[cols].max(1) - df[cols].min(1))/df[cols].min(1))
@jezrael Thank you for suggestion, added to solution.
1

Try this:

def f(a,b,c):
    if( a*b*c==0):
        return 0
    else:
        return (max(a,b,c) - min(a,b,c))/min(a,b,c)

df['D'] = df.apply(lambda x: f(x['A'], x['B'], x['C']), axis=1)

    A   B   C
0  10   7   0
1  10   8  12
2   9   8  11
3  10  11  12
4  13   5   0
5   0   3  10
6  12   8  11
7  12   9   7
8  11  10   9
9  10  11   9
    A   B   C         D
0  10   7   0  0.000000
1  10   8  12  0.500000
2   9   8  11  0.375000
3  10  11  12  0.200000
4  13   5   0  0.000000
5   0   3  10  0.000000
6  12   8  11  0.500000
7  12   9   7  0.714286
8  11  10   9  0.222222
9  10  11   9  0.222222

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.