0

I'm stuck on how to stack/rearrange this dataframe to how I want it. Could someone help me out here? I've tried a bunch of different things with stacking but can't get it right.

I am trying to get it in order like this one, where each country has the info for all of the years, and then it goes to the next country.

0 Albania 1970, 1 Albania 1970, 2 Albania 1971, 3 Albania 1971...10805 Zimbabwe 2015, 10806 Zimbabwe 2016

Here is the code I have:

import pandas as pd

url = 'https://raw.githubusercontent.com/cleibowitz/Module-6/main/Module%206%20Dataset%20-%20co2%20PC%20transposed.csv'

data = pd.read_csv(url, index_col = 'Year')

data.columns.name = 'Country'

data = pd.DataFrame(data.stack().rename('value'))


data = data.reset_index()

data = data.set_index('Country')


data

I'd appreciate it if someone could explain how I can get the result I am looking for.

Laurel
  • 5,771
  • 12
  • 29
  • 54

0 Answers0