pythonstringif-statementconditional-statementscontains

Conditional If Statement: If value in row contains string ... set another column equal to string


EDIT MADE:

I have the 'Activity' column filled with strings and I want to derive the values in the 'Activity_2' column using an if statement.

So Activity_2 shows the desired result. Essentially I want to call out what type of activity is occurring.

I tried to do this using my code below but it won't run (please see screen shot below for error). Any help is greatly appreciated!

enter image description here

    for i in df2['Activity']:
        if i contains 'email':
            df2['Activity_2'] = 'email'
        elif i contains 'conference'
            df2['Activity_2'] = 'conference'
        elif i contains 'call'
            df2['Activity_2'] = 'call'
        else:
            df2['Activity_2'] = 'task'


Error: if i contains 'email':
                ^
SyntaxError: invalid syntax

Solution

  • The current solution behaves wrongly if your df contains NaN values. In that case I recommend using the following code which worked for me

    temp=df.Activity.fillna("0")
    df['Activity_2'] = pd.np.where(temp.str.contains("0"),"None",
                       pd.np.where(temp.str.contains("email"), "email",
                       pd.np.where(temp.str.contains("conference"), "conference",
                       pd.np.where(temp.str.contains("call"), "call", "task"))))