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_topandlego_bottomvertically to make one large complete dataframe. - Name the new dataframe
full_set. - Save the new dimension of
full_setin an object namedfull_set_shape. - Display the new dataframe.
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 aswashed_lego. - Make a new column named
total_piecesby adding up columnsmatteandtransparent. - Sort the dataframe by
total_piecesin descending order. - Save this in an object named
lego_details. - Display the new dataframe.