Learn
Aggregates in Pandas
Review

This lesson introduced you to aggregates in Pandas. You learned:

  • How to perform aggregate statistics over individual rows with the same value using groupby.
  • How to rearrange a DataFrame into a pivot table, a great way to compare data across two dimensions.

Instructions

1.

Let’s examine some more data from ShoeFly.com. This time, we’ll be looking at data about user visits to the website (the same dataset that you saw in the introduction to this lesson).

The data is a DataFrame called user_visits. Use print and head() to examine the first few rows of the DataFrame.

2.

The column utm_source contains information about how users got to ShoeFly’s homepage. For instance, if utm_source = Facebook, then the user came to ShoeFly by clicking on an ad on Facebook.com.

Use a groupby statement to calculate how many visits came from each of the different sources. Save your answer to the variable click_source.

Remember to use reset_index()!

3.

Paste the following code into script.py so that you can see the results of your previous groupby:

print(click_source)
4.

Our Marketing department thinks that the traffic to our site has been changing over the past few months. Use groupby to calculate the number of visits to our site from each utm_source for each month. Save your answer to the variable click_source_by_month.

5.

The head of Marketing is complaining that this table is hard to read. Use pivot to create a pivot table where the rows are utm_source and the columns are month. Save your results to the variable click_source_by_month_pivot.

It should look something like this:

utm_source 1 - January 2 - February 3 - March
email
facebook
google
twitter
yahoo
6.

View your pivot table by pasting the following code into script.py:

print(click_source_by_month_pivot)
Folder Icon

Sign up to start coding

Already have an account?