0

I have the following data set :

enter image description here

and I want to extract a new dataframe of the following form :

enter image description here

I tried this code but I got "NaN" for all cells

years = list(df_le["Year"].unique())
df2 =pd.DataFrame()
df2["Country"] =list(df_le["Country"].unique())
for year in years :
   df2[year] = df_le[df_le["Year"]==year]["Life expectancy "]
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74

1 Answers1

0

Try:

df.pivot(index='Country', columns='Year', values='Life expectancy').reset_index()
Vladimir Fokow
  • 3,728
  • 2
  • 5
  • 27