Aggregates in Pandas Q10

I am getting a KeyError in my last pivot table for is_clicked, however I used is_clicked successfully in previous code. Not sure why this is throwing a KeyError:

import codecademylib import pandas as pd ad_clicks = pd.read_csv('ad_clicks.csv') a_clicks = ad_clicks[ad_clicks.experimental_group == 'A'] b_clicks = ad_clicks[ad_clicks.experimental_group == 'B'] most_views = ad_clicks.groupby('utm_source').user_id.count().reset_index() ad_clicks['is_click'] = ~ad_clicks.ad_click_timestamp.isnull() clicks_by_source = ad_clicks.groupby(['utm_source', 'is_click']).user_id.count().reset_index() clicks_pivot = clicks_by_source.pivot( columns='is_click', index='utm_source', values='user_id' ).reset_index() clicks_by_ad = ad_clicks.groupby(['experimental_group', 'is_click']).user_id.count().reset_index() experimental_clicks = clicks_by_ad.pivot( columns='experimental_group', index='is_click', values='user_id' ).reset_index() clicks_pivot['percent_clicked'] = clicks_pivot[True] / (clicks_pivot[True] + clicks_pivot[False]) a_clicks_pivot = a_clicks.groupby(['is_click', 'day'])\ .user_id.count()\ .reset_index()\ .pivot( columns='is_click', index='day', values='user_id' ).reset_index()

I don’t think the codebyte will work in this instance.
What lesson is this? Is it on the DS or DA path/track?

Edit. Nvm. it’s the Shoefly project.

You need to specify or create a column in your last a_clicks_pivot ['percent_clicked] = ___

I figured it out. I needed to put my data frames a_clicks and b_clicks (lines 5 and 6) down further in my code otherwise it’s not recognizing my variables.

1 Like