FAQ: Aggregates in Pandas - Review

This community-built FAQ covers the "Review " exercise from the lesson “Aggregates in Pandas”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Data Science

Data Analysis with Pandas

FAQs on the exercise _Review _

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

How would I go about deleting the numbers in front of the months if I wanted to make the final pivot table look a little cleaner? For example, take the "1 - " out of “1 - January”.

2 Likes

How can I do the opposite? From pivoted dataframe to panel like dataframe?

1 Like

I think you could apply a lambda function to each string value in the month column (using .apply()) as follows:

tidy_months = lambda x: x.split('- ')[-1]
3 Likes

I tried to write a function to do it.

def unpivot(pivoted_dataframe, second_column_name, third_column_name):
  data = []
  for i, value1 in pivoted_dataframe.iloc[:, 0].iteritems():
    for value2 in pivoted_dataframe.columns[1:]:
      data.append([value1, value2, pivoted_dataframe.loc[i, value2]])
  return pd.DataFrame(data, columns=[pivoted_dataframe.columns[0], second_column_name, third_column_name])

click_source_by_month_unpivot = unpivot(click_source_by_month_pivot, 'month', 'visits')

The names of the second and third columns (month and visits in this example) are missing in the pivoted DataFrame, so I think we need to give them. (Probably the previous DataFrame, click_source_by_month, has a third column named id, but I thought this name would be better to be changed to visits or count.)

If you have any comments or find a better way please let me know.

Recently I learned that there is a method .melt() to reshape a DataFrame to an “unpivoted” one.

click_source_by_month_melt = pd.melt(click_source_by_month_pivot, id_vars='utm_source', var_name='month', value_name='visits')

What’s the difference between

click_source_by_month = user_visits.groupby('utm_source')['month'].count().reset_index()

and

click_source_by_month = user_visits.groupby(['utm_source', 'month']).count().reset_index()

1 Like