Issue
This Content is from Stack Overflow. Question asked by Bijan Khair Rahman
I basically have a dataframe in pandas with a list of countries, but I want to classify each country by continent, and then by region. I want to add two more columns to the original dataframe.
import pandas as pd
import pandasql as psql
import matplotlib.pyplot as plt
import plotly.express as px
from pandasql import sqldf
pysqldf = lambda q: sqldf(q, globals())
suicide_df = pd.read_csv(r"C:UsersslayeDownloadssuicide_ratesmaster.csv")
display(suicide_df)```
country year sex age suicides_no population suicides/100k pop country-year HDI for year gdp_for_year ($) gdp_per_capita ($) generation
0 Albania 1987 male 15-24 years 21 312900 6.71 Albania1987 NaN 2,156,624,900 796 Generation X
1 Albania 1987 male 35-54 years 16 308000 5.19 Albania1987 NaN 2,156,624,900 796 Silent
2 Albania 1987 female 15-24 years 14 289700 4.83 Albania1987 NaN 2,156,624,900 796 Generation X
3 Albania 1987 male 75+ years 1 21800 4.59 Albania1987 NaN 2,156,624,900 796 G.I. Generation
4 Albania 1987 male 25-34 years 9 274300 3.28 Albania1987 NaN 2,156,624,900 796 Boomers
... ... ... ... ... ... ... ... ... ... ... ... ...
27815 Uzbekistan 2014 female 35-54 years 107 3620833 2.96 Uzbekistan2014 0.675 63,067,077,179 2309 Generation X
27816 Uzbekistan 2014 female 75+ years 9 348465 2.58 Uzbekistan2014 0.675 63,067,077,179 2309 Silent
27817 Uzbekistan 2014 male 5-14 years 60 2762158 2.17 Uzbekistan2014 0.675 63,067,077,179 2309 Generation Z
27818 Uzbekistan 2014 female 5-14 years 44 2631600 1.67 Uzbekistan2014 0.675 63,067,077,179 2309 Generation Z
27819 Uzbekistan 2014 female 55-74 years 21 1438935 1.46 Uzbekistan2014 0.675 63,067,077,179 2309 Boomers
for now I've been manually compiling nested dictionaries, but I'm wondering if there's a faster way to do this.
```regions = {'Asia': {'Central Asia', 'South Asia', 'East Asia', 'SouthEast Asia', 'West Asia', 'Middle East'},
'Europe': {'Western Europe', 'Eastern Europe', 'Mediterranean', 'Nordic'},
'North America': {'United States'}```
I apologize if this is a sloppy post, I don't know the formatting on stackoverflow.
Solution
This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.
This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.