Plotting excel data rows in matplotlib

75 Views Asked by At

I have data in an excel file where I need to plot two rows. The excel file has the years in one row, and all the countries in one column ´. The data looks like this:

enter image description here

When trying to plot it I get this key error:

File pandas_libs\hashtable_class_helper.pxi:5206 in pandas._libs.hashtable.PyObjectHashTable.get_item

KeyError: 'TIME'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):

File ~\OneDrive - Danmarks Tekniske Universitet\Courses\Enviromental Modelling\Assigment 0\Assignment 0.py:22 in x = CO2emission['TIME']

File ~\Anaconda3\lib\site-packages\pandas\core\frame.py:3505 in getitem indexer = self.columns.get_loc(key)

File ~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py:3623 in get_loc raise KeyError(key) from err

KeyError: 'TIME'


My code looks like this:

'''

`import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Importing data

studentlist = pd.read_csv(r"C:\Users\cabu\OneDrive - Danmarks Tekniske Universitet\Courses\Enviromental Modelling\Assigment 0\classlist12104.csv",';') #putting r before the string converts it to raw string
ExcelEmission = pd.ExcelFile(r"C:\Users\cabu\OneDrive - Danmarks Tekniske Universitet\Courses\Enviromental Modelling\Assigment 0\eurostat_CO2_emissions.xlsx") 

# Import excel file
CO2emission = pd.read_excel(ExcelEmission, 'Sheet 1',skiprows = 3) # Read specific sheet in the excel file and skipping the first few rows

CO2emission.head()
# Plotting CO2 emission for Denmark


x = CO2emission['TIME']
y = CO2emission['Denmark']`

Anyone knows what goes wrong?

0

There are 0 best solutions below