Search code examples
pythonpandasif-statementmultiple-columns

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


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.


Solution

  • You can do this:

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