Log In

Assignment 3 - Pandas Data Analysis Practice

Open In Colab

As you go through this notebook, you will find a ??? in certain places. To complete this assignment, you must replace all the ??? with appropriate values, expressions or statements to ensure that the notebook runs properly end-to-end.

Some things to keep in mind:

  • Make sure to run all the code cells, otherwise you may get errors like NameError for undefined variables.
  • Do not change variable names, delete cells or disturb other existing code. It may cause problems during evaluation.
  • In some cases, you may need to add some code cells or new statements before or after the line of code containing the ???.
  • Questions marked (Optional) will not be considered for evaluation, and can be skipped. They are for your learning.

This tutorial is an executable Jupyter notebook. Click the Open in Colab button at the top of this page to execute the code.

Jupyter Notebooks: This notebook is made of cells. Each cell can contain code written in Python or explanations in plain English. You can execute code cells and view the results instantly within the notebook. Jupyter is a powerful platform for experimentation and analysis. Don't be afraid to mess around with the code & break things - you'll learn a lot by encountering and fixing errors. You can use the "Kernel > Restart & Clear Output" menu option to clear all outputs and start again from the top.

import pandas as pd

In this assignment, we're going to analyze an operate on data from a CSV file. Let's begin by downloading the CSV file.

from urllib.request import urlretrieve

urlretrieve('https://raw.githubusercontent.com/JovianHQ/notebooks/refs/heads/main/data-analysis-with-python-zero-to-pandas/assignment-3-pandas-practice/countries.csv'
            , "countries.csv")

Let's load the data from the CSV file into a Pandas data frame.

countries_df = pd.read_csv('countries.csv')
countries_df

Q1: How many countries does the dataframe contain?

Hint: Use the .shape method.

num_countries = ???
print('There are {} countries in the dataset'.format(num_countries))

Q2: Retrieve a list of continents from the dataframe?

Hint: Use the .unique method of a series.

continents = ???
continents

Q3: What is the total population of all the countries listed in this dataset?

total_population = ???
print('The total population is {}.'.format(int(total_population)))

Q: (Optional) What is the overall life expectancy across in the world?

Hint: You'll need to take a weighted average of life expectancy using populations as weights.

 
 

Q4: Create a dataframe containing 10 countries with the highest population.

Hint: Chain the sort_values and head methods.

most_populous_df = ???
most_populous_df

Q5: Add a new column in countries_df to record the overall GDP per country (product of population & per capita GDP).

countries_df['gdp'] = ???
countries_df

Q: (Optional) Create a dataframe containing 10 countries with the lowest GDP per capita, among the counties with population greater than 100 million.

 
 

Q6: Create a data frame that counts the number countries in each continent?

Hint: Use groupby, select the location column and aggregate using count.

country_counts_df = ???
country_counts_df

Q7: Create a data frame showing the total population of each continent.

Hint: Use groupby, select the population column and aggregate using sum.

continent_populations_df = ???
continent_populations_df

Let's download another CSV file containing overall Covid-19 stats for various countires, and read the data into another Pandas data frame.

urlretrieve('https://raw.githubusercontent.com/JovianHQ/notebooks/refs/heads/main/data-analysis-with-python-zero-to-pandas/assignment-3-pandas-practice/covid-countries-data.csv',
'covid-countries-data.csv')
covid_data_df = pd.read_csv('covid-countries-data.csv')
covid_data_df

Q8: Count the number of countries for which the total_tests data is missing.

Hint: Use the .isna method.

total_tests_missing = ???
print("The data for total tests is missing for {} countries.".format(int(total_tests_missing)))

Let's merge the two data frames, and compute some more metrics.

Q9: Merge countries_df with covid_data_df on the location column.

*Hint: Use the .merge method on countries_df.

combined_df = ???
combined_df

Q10: Add columns tests_per_million, cases_per_million and deaths_per_million into combined_df.

combined_df['tests_per_million'] = combined_df['total_tests'] * 1e6 / combined_df['population']
combined_df['cases_per_million'] = ???
combined_df['deaths_per_million'] = ???
combined_df

Q11: Create a dataframe with 10 countires that have highest number of tests per million people.

highest_tests_df = ???
highest_tests_df

Q12: Create a dataframe with 10 countires that have highest number of positive cases per million people.

highest_cases_df = ???
highest_cases_df

Q13: Create a dataframe with 10 countires that have highest number of deaths cases per million people?

highest_deaths_df = ???
highest_deaths_df

(Optional) Q: Count number of countries that feature in both the lists of "highest number of tests per million" and "highest number of cases per million".

 
 
 

(Optional) Q: Count number of countries that feature in both the lists "20 countries with lowest GDP per capita" and "20 countries with the lowest number of hospital beds per thousand population". Only consider countries with a population higher than 10 million while creating the list.

 
 
 

Submission

Congratulations on making it this far! You've reached the end of this assignment, and you just completed your first real-world data analysis problem.

To save your work, select "File" > "Save a Copy in Drive" on Google Colab. Once the copy is created, click the "Share" button and select "Anyone with the link" under the "General Access" section to make this notebook publicly accessible.

Then, copy the notebook link and submit it on the assignment page.