Home > OS >  How do I make an if statement in with multiple parameter using excel columns in python csv
How do I make an if statement in with multiple parameter using excel columns in python csv

Time:11-05

So basically I am trying to write a statement where if a value in Col1 = 1 or Col2 = 1 than create a new column with the value 10 and if both Col1 and Col2 = 0 the new column should print 0 or just skip.

so far this is what I did

if df.Col1 == 1 or df.Col2 == 1:
    df['newCol'] = 10
else: pass

This is giving me an error.

CodePudding user response:

You can do this:

df['newCol'] = np.where(((df['Col1']==1)|(df['Col2']==1)), 10,np.nan)

CodePudding user response:

Try this assuming df is your dataframe.. df['newcolumn'] =df.apply(function, axis=1) then in function write def function(row) : if row['col1'] == 1 or row['Col2'] == 1: return 10 (you can pass 0 in else part as well)

  • Related