0

I want to create a new data frame from multiple data frames in one statement.

For Eg:

df.loc[(df[[A,B]].mean(axis=1) <= Const1), 'D'] = df['F']

df.loc[(df[[A,B]].mean(axis=1) >  Const1) & (df['E']<=Const2), 'D'] = df['G']

df.loc[(df[[A,B]].mean(axis=1) >  Const1) & (df['E']> Const2), 'D'] = df['H'] 

TO

df['D'] = 'some statement'
deadshot
  • 8,881
  • 4
  • 20
  • 39
  • 1
    Does this answer your question? [Numpy "where" with multiple conditions](https://stackoverflow.com/questions/39109045/numpy-where-with-multiple-conditions) – Ben.T Jun 17 '20 at 02:49
  • In particular the answer of [Merlin](https://stackoverflow.com/a/39111919/9274732) from the link above – Ben.T Jun 17 '20 at 02:50
  • This wont work ere because 'High" , " Medium" & 'low' are constants in the provided solution. What I need is the value corresponding to some indices from another dataframe with non-constant values. – Nikita Agrawal Jun 17 '20 at 05:19

1 Answers1

0

Using np.select as in the link provided, even if what you want the choices to be series and not constant, it works the same way.

import pandas as pd
import numpy as np

# example data
np.random.seed(0)
df = pd.DataFrame(np.random.randint(0,100, 30).reshape(-1, 6), 
                  columns=list('ABEFGH'))
Const1, Const2 = 50, 50

# conditions
conds = [(df[['A','B']].mean(axis=1) <= Const1), 
         (df[['A','B']].mean(axis=1) >  Const1) & (df['E']<=Const2), 
         (df[['A','B']].mean(axis=1) >  Const1) & (df['E']> Const2)]
# choices that are series
choices = [df['F'], df['G'], df['H']]

#use np.select
df['D'] = np.select(condlist=conds, choicelist=choices)
print(df)
    A   B   E   F   G   H   D
0  44  47  64  67  67   9  67 #value from F
1  83  21  36  87  70  88  70 #value from G
2  88  12  58  65  39  87  65
3  46  88  81  37  25  77  77 #value from H
4  72   9  20  80  69  79  80
Ben.T
  • 29,160
  • 6
  • 32
  • 54