Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Challenge: GDP Growth Rate Calculation | Economic Data Analysis with Python
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Python for Economists

bookChallenge: GDP Growth Rate Calculation

In economic analysis, understanding how a country's Gross Domestic Product (GDP) changes over time is essential. The GDP growth rate measures the percentage increase in economic output from one year to the next, providing insight into economic health and trends. You will often encounter GDP data in tabular form, such as a pandas DataFrame, with rows representing countries and columns representing years. Calculating annual growth rates from such data requires you to compare each year's value to the previous year's, handling any missing values appropriately so that calculations remain accurate and meaningful.

To illustrate, you will work with a hardcoded DataFrame containing GDP values for several countries over three consecutive years. Your task is to write a Python function that computes the annual GDP growth rate for each country, returning a new DataFrame with the calculated growth rates. If a value is missing for a specific year or country, your function should skip the calculation for that year, avoiding errors or misleading results.

12345678910111213141516171819
import pandas as pd # Example DataFrame with GDP values (in billions USD) data = { "Country": ["CountryA", "CountryB", "CountryC"], "2019": [2100, 1800, None], "2020": [2200, None, 1500], "2021": [2300, 2000, 1600] } gdp_df = pd.DataFrame(data) gdp_df.set_index("Country", inplace=True) def calculate_gdp_growth(df): # Calculate year-over-year growth rates, skipping missing values growth_df = df.pct_change(axis=1) * 100 return growth_df growth_rates = calculate_gdp_growth(gdp_df) print(growth_rates)
copy

This code defines a calculate_gdp_growth function that takes a DataFrame of GDP values and computes the year-over-year percentage change for each country. The function uses the pandas pct_change method along columns (years), multiplying by 100 to express the result as a percentage. Missing values are handled automatically—when data is missing for a particular year, the growth rate is not calculated for that year, and the result is represented as NaN in the output DataFrame.

Note
Definition

Definition: The GDP growth rate for a given year is calculated as (GDP in current year - GDP in previous year) / GDP in previous year * 100. This expresses the change as a percentage of the previous year's GDP.

By structuring your data and calculations in this way, you ensure that your analysis remains robust even when some data points are missing. This approach is common in real-world economic datasets, where gaps in reporting or collection can occur.

Aufgabe

Swipe to start coding

Write a function named calculate_gdp_growth that:

  • Accepts a pandas DataFrame with countries as the index and years as columns, containing GDP values (may include missing values).
  • Returns a new DataFrame of the same shape, where each cell contains the annual GDP growth rate (as a percentage) for that country and year, or NaN if the calculation cannot be performed due to missing data.
  • Uses pandas' built-in functions to handle the calculations efficiently. Test your function using the provided example DataFrame, and verify that missing values are handled as described.

Lösung

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 3
single

single

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Suggested prompts:

Can you explain how the pct_change function works in pandas?

What should I do if I want to include more years or countries in the DataFrame?

How can I interpret the NaN values in the growth rates output?

close

bookChallenge: GDP Growth Rate Calculation

Swipe um das Menü anzuzeigen

In economic analysis, understanding how a country's Gross Domestic Product (GDP) changes over time is essential. The GDP growth rate measures the percentage increase in economic output from one year to the next, providing insight into economic health and trends. You will often encounter GDP data in tabular form, such as a pandas DataFrame, with rows representing countries and columns representing years. Calculating annual growth rates from such data requires you to compare each year's value to the previous year's, handling any missing values appropriately so that calculations remain accurate and meaningful.

To illustrate, you will work with a hardcoded DataFrame containing GDP values for several countries over three consecutive years. Your task is to write a Python function that computes the annual GDP growth rate for each country, returning a new DataFrame with the calculated growth rates. If a value is missing for a specific year or country, your function should skip the calculation for that year, avoiding errors or misleading results.

12345678910111213141516171819
import pandas as pd # Example DataFrame with GDP values (in billions USD) data = { "Country": ["CountryA", "CountryB", "CountryC"], "2019": [2100, 1800, None], "2020": [2200, None, 1500], "2021": [2300, 2000, 1600] } gdp_df = pd.DataFrame(data) gdp_df.set_index("Country", inplace=True) def calculate_gdp_growth(df): # Calculate year-over-year growth rates, skipping missing values growth_df = df.pct_change(axis=1) * 100 return growth_df growth_rates = calculate_gdp_growth(gdp_df) print(growth_rates)
copy

This code defines a calculate_gdp_growth function that takes a DataFrame of GDP values and computes the year-over-year percentage change for each country. The function uses the pandas pct_change method along columns (years), multiplying by 100 to express the result as a percentage. Missing values are handled automatically—when data is missing for a particular year, the growth rate is not calculated for that year, and the result is represented as NaN in the output DataFrame.

Note
Definition

Definition: The GDP growth rate for a given year is calculated as (GDP in current year - GDP in previous year) / GDP in previous year * 100. This expresses the change as a percentage of the previous year's GDP.

By structuring your data and calculations in this way, you ensure that your analysis remains robust even when some data points are missing. This approach is common in real-world economic datasets, where gaps in reporting or collection can occur.

Aufgabe

Swipe to start coding

Write a function named calculate_gdp_growth that:

  • Accepts a pandas DataFrame with countries as the index and years as columns, containing GDP values (may include missing values).
  • Returns a new DataFrame of the same shape, where each cell contains the annual GDP growth rate (as a percentage) for that country and year, or NaN if the calculation cannot be performed due to missing data.
  • Uses pandas' built-in functions to handle the calculations efficiently. Test your function using the provided example DataFrame, and verify that missing values are handled as described.

Lösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 3
single

single

some-alt