Home > database >  If..else use different input datasets
If..else use different input datasets

Time:06-09

I'm a python beginner could you please help me to this?

If variable is true I need to use A Dataset as input else have to use B dataset.

How to write this please help me.

CodePudding user response:

What do you mean by "use" ? I guess what you are looking for is np.where(condition, do if true, do if false) ?

Example from numpy doc:

a = np.array([[0, 1, 2],
              [0, 2, 4],
              [0, 3, 6]])

np.where(a < 4, a, -1)

>>> array([[ 0,  1,  2],
           [ 0,  2, -1],
           [ 0,  3, -1]])

CodePudding user response:

You could create a third variable df and assign that to a copy of A or B and use that for the code:

if variable:
    df = A.copy()
else:
    df = B.copy()

##some code using df##

Alternatively, you could write a function for the code using the df, and have A or B as an input like this:

def df_function(df):
    ##some code using df##

if variable:
    df_function(A)
else:
    df_function(B)
  • Related