There two sources of data that need to be merged : population_df = pd.read_csv('https://raw.githubusercontent.com/Explore-AI/Public-Data/master/AnalyseProject/world_population.csv', index_col='Country Code') meta_df = pd.read_csv('https://raw.githubusercontent.com/Explore-AI/Public-Data/master/AnalyseProject/metadata.csv', index_col='Country Code') Your solution has one source of data (i.e. data = pd.read_csv('world_population.csv') The solution does not give the expected output
There two sources of data that need to be merged :
population_df = pd.read_csv('https://raw.githubusercontent.com/Explore-
meta_df = pd.read_csv('https://raw.githubusercontent.com/Explore-AI/Public-Data/master/AnalyseProject/metadata.csv', index_col='Country Code')
Your solution has one source of data (i.e. data = pd.read_csv('world_population.csv')
The solution does not give the expected output
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 2 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
Coded as advised:
import numpy as np
import pandas as pd
def get_total_pop_by_income(income_group_name='Low income'):
population_df = pd.read_csv('https://raw.githubusercontent.com/Explore-
meta_df = pd.read_csv('https://raw.githubusercontent.com/Explore-AI/Public-Data/master/AnalyseProject/metadata.csv', index_col='Country Code')
# Merge population and metadata dataframes
df = pd.merge(population_df, meta_df[['Income Group']], left_index=True, right_index=True)
# Filter by income group
df = df[df['Income Group'] == income_group_name]
if len(df) == 0:
raise ValueError(f"No data found for income group {income_group_name}")
# Convert year and population columns to numpy arrays
year_col = df.columns.values[range(0, len(df.columns.values), 2)]
population_col = df.columns.values[range(1, len(df.columns.values), 2)]
year_array = np.array(year_col, dtype=np.int64)
population_array = np.array(df[population_col], dtype=np.int64)
# Stack arrays horizontally
result = np.hstack((year_array.reshape(-1, 1), population_array))
return result
Calling the function :
data =get_total_pop_by_income('High income')
Error message :-
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)