Practice and reinforce the concepts from Lesson 19
In this activity, you will:
:computer: Activity Type: Coding Exercise
⏱️ Total Time: 30-40 minutes
:emoji: Platform: Google Colab
Complete the exercises in this Google Colab notebook: https://colab.research.google.com/drive/1TLgmP8CStA7oQd33V47U1wI_4LJc6ibB?usp=sharing
:warning: Important Setup Steps
- Make a copy of the Colab file before you start coding
- Click "File" -> "Save a copy in Drive"
- Work in YOUR COPY, not the template file
- Rename your copy to include your name: "Pandas_Joining_YourName"
⏱️ Estimated Time: 15-20 minutes
You'll combine height and weight data from two separate dictionaries into a single DataFrame.
Create the data structures
Convert to DataFrames
df_height
df_weight
Explore your data
print()
or just the variable nameSet the index
set_index()
to set the "first name"
column as the index for both DataFramesJoin the DataFrames
join()
method to combine the DataFrames:bulb: Helpful Hint Remember that when you set an index, it becomes the "key" that pandas uses to match rows between DataFrames. Make sure both DataFrames have the same index column before joining!
⏱️ Estimated Time: 15-20 minutes
You'll work with CSV files to practice joining data from external sources.
Upload the CSV files
age.csv
and races.csv
to your Colab environmentRead the CSV files
pd.read_csv()
to load age.csv
into a DataFrame called age
pd.read_csv()
to load races.csv
into a DataFrame called races
Examine the data
"Name"
columnPrepare for joining
"Name"
column as the index for the age
DataFrame"Name"
column as the index for the races
DataFramePerform the join
join()
method to combine the DataFrames:bulb: Common Challenge If you get an error when uploading files, try refreshing the Colab page and uploading again. Make sure the file names match exactly (including capitalization).
"KeyError" when joining:
CSV files won't upload:
Join results in missing data:
how='outer'
parameter in join to see all data:white_check_mark: Checklist:
:information_source: Submit Your Work :memo: Ready to submit? Make sure you've:
- Completed both exercises
- Saved your Colab notebook
- Reviewed your code and outputs
:link: Submit your exercise here
:alarm_clock: Submission deadline: Check with your instructor