I import three csv-files with characteristics of countries (rows) and years (columns):
country_data_m = 'country_data_m.csv'
m_year = pd.read_csv(country_data_m, nrows=161, index_col=0, header=0, sep=';', na_values=[""])
country_data_e = 'country_data_e.csv'
e_year = pd.read_csv(country_data_e, nrows=161, index_col=0, header=0, sep=';', na_values=[""])
country_data_i = 'country_data_i.csv'
i_year = pd.read_csv(country_data_i, nrows=161, index_col=0, header=0, sep=';', na_values=[""])
The datasets look like this:
1995 1996 1997 \
Afghanistan NaN NaN NaN
Albania NaN NaN NaN
Angola 5.538749e+09 7.526447e+09 7.648377e+09
Antigua and Barbuda 5.772807e+08 6.337306e+08 6.806171e+08
1995 1996 1997 1998 1999 \
Afghanistan NaN NaN NaN NaN NaN
Albania NaN NaN NaN NaN NaN
Angola 0.8565 0.8369 0.8173 0.7976 0.7777
Antigua and Barbuda 0.6957 0.6352 0.6513 0.6401 0.6171
1995 1996 1997 1998 1999 \
Afghanistan NaN NaN NaN NaN NaN
Albania NaN NaN NaN NaN NaN
Angola 0.0612 0.0626 0.0641 0.0655 0.0670
Antigua and Barbuda 0.1852 0.2264 0.2147 0.2147 0.2030
For each country, I need a dictionary, where the key is the year, and where the values are the variables from the three different datasets. So far, I tried this code:
afghanistan = {m_year.loc["Afghanistan", (year)],e_year.loc["Afghanistan", (year)], i_year.loc["Afghanistan", (year)]): year for year in range(1995, 2017)}
albania = {m_year.loc["Albania", (year)],e_year.loc["Albania", (year)], i_year.loc["Albania", (year)]): year for year in range(1995, 2017)}
...
zimbabwe = {m_year.loc["Zimbabwe", (year)],e_year.loc["Zimbabwe", (year)], i_year.loc["Zimbabwe", (year)]): year for year in range(1995, 2017)}
However, the code cannot find the year in the dataframes and gives me the following error:
TypeError: cannot do label indexing on <class 'pandas.core.indexes.base.Index'> with these indexers [1995] of <class 'int'>
Any help would be very much appreciated!
.loc
which should be index, whereas it is the actual value of the year. please provide the columns of each of these datasets. and what you actually want as an outcome $\endgroup$