0

I want the year as the column name and index as the country with values of 'Cigarette consumption per smoker per day (cigarettes)' but cannot figure out a way. Here is the df.head() note: there are many countries in the dataframe

     Entity         Year    Cigarette consumption  
0   Afghanistan 1980    5.700000  
1   Afghanistan 1981    5.800000  
2   Afghanistan 1982    5.800000  
3   Afghanistan 1983    5.900000  
4   Afghanistan 1984    6.000000  
5   Afghanistan 1985    6.100000  
6   Afghanistan 1986    6.100000  
7   Afghanistan 1987    6.100000  
8   Afghanistan 1988    6.000000  
9   Afghanistan 1989    5.900000  
10  Afghanistan 1990    5.800000  
11  Afghanistan 1991    5.600000  

I have tried using set_index and set_values function and then transposing but no luck. CSV file link - blob:https://ourworldindata.org/57647092-5c4c-4635-b937-ee35ed60f99e

i want something like this Years as column names

               1980     1981     1982     1983     1984     1985      1986 
Afghanistan 5.700000 5.800000 5.800000 5.900000 6.000000 6.100000  6.100000   
(Another Country)
anky
  • 74,114
  • 11
  • 41
  • 70
  • `df.set_index(['Entity','Year']).unstack()` or `df.pivot_table(index='Entity',columns='Year')` – anky Aug 10 '19 at 13:49

1 Answers1

0

try this

df2=df.pivot_table(index='Entity',columns='Year',values='Cigarette consumption').reset_index()
df2.columns.name=''
tawab_shakeel
  • 3,701
  • 10
  • 26