How to apply if condition to two different columns and put the result to a new column

Question:

I have a data frame df2 and want to generate a new column called ‘tag’ based on a if logic on two existing columns.

import pandas as pd
df2 = pd.DataFrame({'NOTES': ["PREPAID_HOME_SCREEN_MAMO","SCREEN_MAMO",
                              "> Unable to connect internet>4G Compatible>Set",
                              "No>Not Barred>Active>No>Available>Others>",
                              "Internet Not Working>>>Unable To Connect To"], 
     'col_1': ["voice", "voice","data","other","voice"],
     'col_2': ["DATA", "voice","VOICE","VOICE","voice"]})

The logic and my attempt are:

df2['Tag'] =             
            if df['col_1']=='data':
                return "Yes"
            elif df['col_2']:
                return "Yes"
            else:
                return "No"

But I got a syntax error:
enter image description here

Asked By: Virendra Patel

||

Answers:

The problem is that you are trying to assign a value with if-statement, which causes the syntax error.

There are many ways to do this, I provide one using pandas.DataFrame.apply.

trans_fn = lambda row: "Yes" if row['col_1']=='data' && row['col_2'] else "No"
df2['tag'] = df2.apply(trans_fn, axis=1) # apply trans_fn to each row
Answered By: ILS
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.