Reputation: 13
I have a dataframe where I want to pass a function to a specific column:
Looking to change the 'Channel' column values based on certain requirements:
if 'Facebook' or 'FacebookPaid' or 'FacebookFree' in df['utm_Campaign']:
df['Channel'] = 'FB'
elif 'Newsletter' in df['utm_Campaign']:
df['Channel'] = 'Email'
else:
if 'YoutubePaid' in df['utm_Campaign']:
df['Channel'] = 'Youtube Direct'
Basically if there is a word 'Facebook' contained in another column (utm_Campaign) from the same row, the string in the 'Channel' column for the dataframe is set as 'FB'.
Not sure what to do. Do i map a function, or do a df.replace, or something else?
Upvotes: 1
Views: 105
Reputation: 295
Just in case you wanted to use apply:
df['Channel'] = df.apply(lambda df: \
'FB' if df['utm_Campaign'].str.contains('Facebook') else \
('Email' if df['utm_Campaign'].str.contains('Newsletter') else \
('Youtube Direct' if df['utm_Campaign'].str.contains('YoutubePaid'))))
Upvotes: 0
Reputation: 1130
You could also do something like this:
df['Channel'] = ['FB' if 'Face' in row else df.iloc[i].Channel for i,row in enumerate(df.utm_Campaign)]
Upvotes: 0
Reputation: 863741
You can create conditions by str.contains
and then use numpy.select
:
m1 = df['utm_Campaign'].str.contains('Facebook|FacebookPaid|FacebookFree')
m2 = df['utm_Campaign'].str.contains('Newsletter')
m3 = df['utm_Campaign'].str.contains('YoutubePaid')
df['Channel'] = np.select([m1,m2,m3], ['FB','Email','Youtube Direct'], default='no match')
Upvotes: 1