It looks like you are trying to create a plot and encountering a KeyError related to the 'country' column. The error is occurring because the 'country' column cannot be found in the data_frames_combined
DataFrame. To resolve this issue, please ensure that:
- The 'country' column exists in the original data source.
- If the column exists but has a different name, make sure to update the column name in the code accordingly.
If you provide the first few rows of your data_frames_combined
DataFrame or information about its structure, I can help you troubleshoot the issue further. You can do this by using the head()
function:
pythonprint(data_frames_combined.head())
This will give you a better understanding of the available columns in your DataFrame, allowing you to identify any discrepancies in the column names.
To create an interactive plot of GDP per capita over time for different countries, you can use the plotly
library. First, you'll need to install it if you haven't already:
bashpip install plotly
Then, modify your code to create the interactive plot:
pythonimport plotly.express as px
# Convert the index 'country' back to a column
data_frames_combined = data_frames_combined.reset_index()
# Create an interactive plot of GDP per capita over time for different countries
fig = px.line(data_frames_combined, x="year", y="GDP_per_capita", color="country", title="GDP per capita over time")
# Show the plot
fig.show()
In this code snippet, we first reset the index so that 'country' becomes a column again. Then, we use the plotly.express
module to create an interactive line plot with the year on the x-axis, GDP per capita on the y-axis, and a separate line for each country. Finally, we display the plot using fig.show()
.
To create an interactive graph where you can choose the country from a dropdown menu, you can use the ipywidgets
library in combination with plotly.graph_objects
. First, you'll need to install ipywidgets
if you haven't already:
bashpip install ipywidgets
Now, modify your code to create the interactive graph with a country selection dropdown:
pythonimport plotly.graph_objects as go
from ipywidgets import interact
# Function to create the plot for the selected country
def plot_gdp_per_capita(country):
# Filter the data for the selected country
country_data = data_frames_combined[data_frames_combined['country'] == country]
# Create the plot
fig = go.Figure()
fig.add_trace(go.Scatter(x=country_data['year'], y=country_data['GDP_per_capita'],
mode='lines+markers', name=country))
# Set the plot title and axis labels
fig.update_layout(title=f'GDP per capita over time for {country}',
xaxis_title='Year',
yaxis_title='GDP per capita')
# Show the plot
fig.show()
# Create a dropdown menu to select a country
country_list = data_frames_combined['country'].unique().tolist()
country_list.sort()
interact(plot_gdp_per_capita, country=widgets.Dropdown(options=country_list, value=country_list[0], description='Country:'))
In this code snippet, we define a function plot_gdp_per_capita
that takes a country name as an input and creates a line plot of GDP per capita over time for that country using plotly.graph_objects
. Then, we create a dropdown menu with the ipywidgets.interact
function, where the menu options are the unique countries in the data_frames_combined
DataFrame. When you select a country from the dropdown menu, the plot_gdp_per_capita
function is called with the selected country, and the corresponding plot is displayed.
To create an interactive plot comparing the percentage of people working in agriculture to the GDP per capita for a selected country, you can modify the plot_gdp_per_capita
function like this:
pythondef plot_gdp_per_capita_and_agri_empl(country):
# Filter the data for the selected country
country_data = data_frames_combined[data_frames_combined['country'] == country]
# Create the plot
fig = go.Figure()
# Add GDP per capita trace (left y-axis)
fig.add_trace(go.Scatter(x=country_data['year'], y=country_data['GDP_per_capita'],
mode='lines+markers', name='GDP per capita'))
# Add percentage of people working in agriculture trace (right y-axis)
fig.add_trace(go.Scatter(x=country_data['year'], y=country_data['Empl_in_agri_in_%_of_all_empl'],
mode='lines+markers', name='Employment in agriculture (%)',
yaxis='y2'))
# Set the plot title and axis labels
fig.update_layout(
title=f'GDP per capita and employment in agriculture for {country}',
xaxis_title='Year',
yaxis_title='GDP per capita',
yaxis2=dict(
title='Employment in agriculture (%)',
overlaying='y',
side='right'
)
)
# Show the plot
fig.show()
# Create a dropdown menu to select a country
interact(plot_gdp_per_capita_and_agri_empl, country=widgets.Dropdown(options=country_list, value=country_list[0], description='Country:'))
In this modified function, we add a second trace to the plot for the percentage of people working in agriculture, and we set its yaxis
property to 'y2'
. Then, we update the layout to include a second y-axis on the right side of the plot for the new trace, with the overlaying
property set to 'y'
so that it shares the same x-axis with the GDP per capita trace.
When you select a country from the dropdown menu, the plot_gdp_per_capita_and_agri_empl
function is called with the selected country, and the corresponding plot comparing the percentage of people working in agriculture to the GDP per capita is displayed.