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
Step by step
Solved in 2 steps
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 :-