'getting raise KeyError(key) from err KeyError: 'Year' from code given below [duplicate]
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
Solution 1:[1]
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")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Albert Winestein |
