6.1. Exercises

Concat questions

Coding questions

Instructions: Running a coding exercise for the first time could take a bit of time for everything to load. Be patient, it could take a few minutes.

When you see ____ in a coding exercise, replace it with what you assume to be the correct code. Run it and see if you obtain the desired output. Submit your code to validate if you were correct.

Make sure you remove the hash (#) symbol in the coding portions of this question. We have commented them so that the line won’t execute and you can test your code after each step.

Concatenating Vertically

Sometimes we accumulate additional data that we need to combine with our existing data. In the following question, we need to combine our dataframes to have a complete collection of all the Lego sets that exist.

Tasks:

  • Combine the two dataframes lego_top and lego_bottom vertically to make one large complete dataframe.
  • Name the new dataframe full_set.
  • Save the new dimension of full_set in an object named full_set_shape.
  • Display the new dataframe.
Hint 1
  • Are you using pd.concat()?
  • Are you concatenating in the correct order with lego_top first and lego_bottom second?
  • Are you putting your dataframes within square brackets?
  • Are you using axis=0
  • Are you using .shape to find the dimension of the new dataframe?
Fully worked solution:


Concatenating Horizontally

Our goal is to obtain a dataframe with the lego_set names and the total amount of pieces in each set but we only have 2 Lego dataframes (with the same indexes). One dataframe has the set names and the other contains information amount the number of matte and transparent pieces included in each set. Complete this question by using pd.concat() and techniques we learned in the previous model.

Tasks:

  • Combine the two dataframes horizontally to make 1 large complete dataframe and name the new dataframe lego_full.
  • Drop any duplicated columns using .loc[] and .duplicate()and save this new dataframe as washed_lego.
  • Make a new column named total_pieces by adding up columns matte and transparent.
  • Sort the dataframe by total_pieces in descending order.
  • Save this in an object named lego_details.
  • Display the new dataframe.
Hint 1
  • Are you using pd.concat()?
  • Are you concatenating the dataframes lego_base with lego_opacity?
  • Are you putting your dataframes within square brackets?
  • Are you removing any duplicated columns?
  • Are you using axis=1?
  • Are you using .assign() to make a new column named total_pieces?
  • Are you using using .sort_values() with the argument ascending=False
Fully worked solution: