Home > Mobile >  find the top-10 countries having the highest gdp per capita in each year. Produce the following tabl
find the top-10 countries having the highest gdp per capita in each year. Produce the following tabl

Time:12-21

this is how my data looks like

    Country Name    1960    1961    1962    1963    1964    1965    1966    1967    1968    ... 2008    2009    2010    2011    2012    2013    2014    2015    2016    2017
0   Aruba   NaN NaN NaN NaN NaN NaN NaN NaN NaN ... 27546.899390    24631.434860    24271.940421    25324.720362    NaN NaN NaN NaN NaN NaN
1   Afghanistan 59.777327   59.878153   58.492874   78.782758   82.208444   101.290471  137.899362  161.322000  129.506654  ... 373.361116  445.893298  553.300289  603.537023  669.009051  638.612543  629.345250  569.577923  561.778746  585.850064
2   Angola  NaN NaN NaN NaN NaN NaN NaN NaN NaN ... 3868.579014 3347.844900 3531.416878 4299.008136 4539.467689 4804.633826 4707.578098 3683.552607 3308.772828 4170.312280
3   Albania NaN NaN NaN NaN NaN NaN NaN NaN NaN ... 4370.539647 4114.136545 4094.358832 4437.178068 4247.614308 4413.081697 4578.666728 3952.830781 4131.872341 4537.862492
4   Andorra NaN NaN NaN NaN NaN NaN NaN NaN NaN ... 47785.659086    43339.379875    39736.354063    41098.766942    38391.080867    40619.711298    42294.994727    36038.267604    37231.815671    39146.548836
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
259 Kosovo  NaN NaN NaN NaN NaN NaN NaN NaN NaN ... 3254.860674 3209.694109 3283.211938 3733.491539 3600.815825 3877.758936 4054.721339 3574.174030 3697.548026 3893.969824
260 Yemen, Rep. NaN NaN NaN NaN NaN NaN NaN NaN NaN ... 1203.720733 1093.812918 1309.231935 1349.420222 1421.170984 1580.181689 1647.033586 1285.563019 660.280885  NaN
261 South Africa    433.941194  444.896268  461.798232  498.601747  534.374176  568.348265  603.069811  654.626304  688.830622  ... 5695.057860 5831.115574 7275.382112 7976.466077 7478.227665 6822.524760 6433.944544 5746.681127 5280.017633 6160.734569
262 Zambia  234.166194  221.728401  213.894229  214.852954  243.105320  303.882532  343.912055  360.770244  410.486261  ... 1369.068249 1139.110233 1463.213573 1644.619672 1734.930612 1850.793359 1738.088202 1313.889646 1262.989682 1509.797013
263 Zimbabwe    280.994586  283.315869  279.440989  280.566213  285.053174  297.363437  281.490800  297.231875  304.982159  ... 325.678570  624.272242  719.979516  840.949877  968.163875  1026.388292 1031.104614 1033.415841 1029.076649 1079.608291

i want the output as follows

    1960    1961       1962         1963          1964  ......  ..... 2017
1   Galbon  United     United       United        kuwait              Monaco
            States     States       States  

2  Niger    North      North       North          United             Lieche
            America    America     America        States
..... ... ..................................................................

this is just the sample output here is the image of the ouput

enter image description here

df.nlargest(10,columns=col_list)

I have tried this so far i don't know how to replace columns with the country name

CodePudding user response:

Use DataFrame.set_index with Series.nlargest per columns in DataFrame.apply:

df1 = df.set_index('Country Name').apply(lambda x: pd.Series(x.nlargest(10).index))
  • Related