0

I want to plot three lines for Turkey, the UK and OECD through the years but those countries are not columns so I am suffering finding a way plot them.

I get this df via

df = df.loc[df["Variable"].eq("Relative advantage") & df["Country"].isin(["United Kingdom", "Türkiye", "OECD - Total"])]

Year Country Value
1990 Turkiye 20
1980 UK 34
1992 UK 32
1980 OECD 29
1992 OECD 23
Nick ODell
  • 15,465
  • 3
  • 32
  • 66
Korkut
  • 39
  • 4

1 Answers1

1

You can use the pivot_table() method to do this. An example:

import pandas as pd

# Set up example dataframe
df = pd.DataFrame([
    [1990,'Turkiye',20],
    [1992,'Turkiye',22],
    [1990,'UK',34],
    [1992,'UK',32],
    [1990,'OECD',29],
    [1992,'OECD',23],
], columns=["year", "country", "value"])

# Pivot so countries are now columns
table = df.pivot_table(values='value', columns='country', index='year')

This creates a dataframe where the countries are columns:

country  OECD  Turkiye  UK
year                      
1990       29       20  34
1992       23       22  32

(I changed some of the dates to make it work out a bit more nicely.)

Then I plot it:

plot of fictional data for three countries

Nick ODell
  • 15,465
  • 3
  • 32
  • 66