0

I am working with a dataset like this, where the values of 'country name' are repeat several time, and 'Indicator name' to.

enter image description here

I want to create a new dataset with its columns are like that

Year  CountryName IndicatorName1 IndicatorName2 ... IndicatorNameX
2000.  USA.       value1.        value2.            valueX
2000.  Canada.    value1.        value2.            valueX    
2001.  USA.       value1.        value2.            valueX
2001.  Canada.    value1.        value2.            valueX 

it is possible to do that??

Thanks in advances!

Maite
  • 65
  • 2
  • 11
  • 1
    Does this answer your question? [How can I pivot a dataframe?](https://stackoverflow.com/questions/47152691/how-can-i-pivot-a-dataframe) – Chris Jan 04 '22 at 20:07

1 Answers1

0

You can use pivot as suggested by @Chris but you can also try:

out = df.set_index(['Country Name', 'Indicator Name']).unstack('Country Name').T \
        .rename_axis(index=['Year', 'Country'], columns=None).reset_index()
print(out)

# Output
   Year Country  IndicatorName1  IndicatorName2
0  2000  France               1               3
1  2000   Italy               2               4
2  2001  France               5               7
3  2001   Italy               6               8

Setup a Pandas / MRE:

data = {'Country Name': ['France', 'Italy', 'France', 'Italy'],
        'Indicator Name': ['IndicatorName1', 'IndicatorName1', 
                           'IndicatorName2', 'IndicatorName2'],
        2000: [1, 2, 3, 4],
        2001: [5, 6, 7, 8]}
df = pd.DataFrame(data)
print(df)

# Output
  Country Name  Indicator Name  2000  2001
0       France  IndicatorName1     1     5
1        Italy  IndicatorName1     2     6
2       France  IndicatorName2     3     7
3        Italy  IndicatorName2     4     8
Corralien
  • 109,409
  • 8
  • 28
  • 52