I am new to data science and I am currently practicing to improve my skills. I used a data set from kaggle and planned how to present the data and came across a problem.
What I was trying to achieve is to insert data to different data frames using a for loop. I have seen an example of this and used the dictionary to save data frames but the data on the data frame is overwritten.
I have a list of data frames:
continents_list = [african_countries, asian_countries, european_countries, north_american_countries,
south_american_countries, oceanian_countries]
This is an example of my data frame from one of the continents:
Continent Country Name Country Code 2010 2011 2012 2013 2014
7 Oceania Australia AUS 11.4 11.4 11.7 12.2 13.1
63 Oceania Fiji FJI 20.1 20.1 20.2 19.6 18.6
149 Oceania New Zealand NZL 17.0 17.2 17.7 15.8 14.6
157 Oceania Papua New Guinea PNG 5.4 5.3 5.4 5.5 5.4
174 Oceania Solomon Islands SLB 9.1 8.9 9.3 9.4 9.5
I first selected the whole row for the country which has the highest rate on a year:
def select_highest_rate(continent, year):
highest_rate_idx = continent[year].idxmax()
return continent.loc[highest_rate_idx]
then created a for loop which creates different data frames for each separate years which must contain all the continent and its corresponding country and rate on that year:
def show_highest_countries(continents_list):
df_highest_countries = {}
years_list = ['2010','2011','2012','2013','2014']
for continent in continents_list:
for year in years_list:
highest_country = select_highest_rate(continent, year)
highest_countries = highest_country[['Continent','Country Name',year]]
df_highest_countries[year] = pd.DataFrame(highest_countries)
return df_highest_countries
here is what it returns: different data frames but only for the last continent
Question: How do I save all the data(continents) on the same data frame? Is it not possible with dictionaries?