Skip to content

Instantly share code, notes, and snippets.

@mmas
Created July 4, 2017 23:54
Show Gist options
  • Save mmas/627f0ceae8e468bde92b1a0fb9ed5f05 to your computer and use it in GitHub Desktop.
Save mmas/627f0ceae8e468bde92b1a0fb9ed5f05 to your computer and use it in GitHub Desktop.
def get_visits(groupby):
ip_visits = data.groupby(groupby)['ip']
return pd.DataFrame(
{'unique_visits': ip_visits.apply(lambda x: len(set(x))),
'total_visits': ip_visits.apply(len)})
data['date_day'] = data['date'].apply(datetime.date)
visits_by_date = get_visits('date_day')
# Redefine with date index to avoid lack of dates.
visits_by_date = pd.DataFrame(visits_by_date,
index=pd.date_range(date_0, date_1)).fillna(0)
# Rest of filtered visits.
visits_by_os = get_visits('os')
visits_by_city = get_visits('city')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment