As you will understand I am pretty new to pandas and I have found myself stuck with the below problem. Lets say I have the below Facebook data ( I have completely randomised them by the way for the sake of the example):
Ad Set Name Impresions Link Clicks
0 253-Page.Visitors.10.Days 100 3
1 254-Cart.Abandoners.10.Days 300 9
2 253-Page.Visitors.10.Days 900 27
3 256-LAL.5%.Add.to.Cart 2,700 81
4 256-LAL.5%.Freq.Visits 8,100 243
5 254-Cart.Abandoners.10.Days 24,300 729
6 254-Cart.Abandoners.10.Days 72,900 2,187
Now what I want to do is to create a new column called 'audience' and populate it based on these 3 conditions:
- if the column 'Ad Set Name' contains the word 'Page.Visitors' the respective cell in the new column audience should be populated with 'Page Visitors'
- If it contains 'Cart.Abandoners' it should be populated with 'Cart Abandoners'
- And finally, if it contains 'LAL' it should be populated with 'Lookalikes'
This is how I tried to do it:
for i in data['Ad Set Name']:
if 'Page.Visitors' in i:
data.loc[i,'audiience'] = 'Page Visitors'
elif 'Cart.Abandoners' in i:
data.loc[i,'audience'] = 'Cart Abandoners'
else:
data.loc[i,'audience'] = 'Lookalikes'
data.head()
but the column I get back is filled with NaN.
Any help would be much appreciated!