0

i get this error from the code provided below :

raise KeyError(key) from err KeyError: 'Year'

code:

import pandas as pd
import matplotlib.pyplot as plt
import sys
import matplotlib
matplotlib.use('Agg')

mark_base = {"Math": [99, 98, 97, 96, 93, 92], "Science": [96, 94, 93, 90, 86, 84]}
mark_data = pd.DataFrame(data=mark_base)

mark_chart = pd.read_csv('C:/Users/naman/OneDrive/Desktop/amaiboy/Visual Studio Code/HTML, CSS and JavaScript/markbase.csv', header=0, sep=",")
mark_chart.plot(x="Percentage", y="Year", kind="line")

plt.ylim(ymin=0)
plt.xlim(xmin=0)

plt.show()
plt.savefig(sys.stdout.buffer)
sys.stdout.flush()

csv file:

Percentage, Year
92, 2017
95, 2018
97, 2019
96, 2020
96, 2021
99, 2022
James Z
  • 12,209
  • 10
  • 24
  • 44

1 Answers1

0

The CSV file column separator is not the default comma but comma-space.

Therefore you either need to remove the extraneous spaces in the CSV file or:

mark_chart = pd.read_csv('C:/Users/naman/OneDrive/Desktop/amaiboy/Visual Studio Code/HTML, CSS and JavaScript/markbase.csv', header=0, sep=",\s")
DarkKnight
  • 19,739
  • 3
  • 6
  • 22