KeyError(f"None of [{key}] are in the [{axis_name}]")

Viewed 40

I want to plot graph using matplotlib for the following dataset.

enter image description here

But I an getting this error.

None of [Index(['roberta_neg', 'roberta_neu', 'roberta_pos'], dtype='object')] are in the [columns]. 

the code that i have used is:

df.plot(x="Ratings", y=["roberta_neg", "roberta_neu","roberta_pos"], kind="bar")
1 Answers

I believe there is a problem with the data you are loading. The error shown happens when the there is a mismatch between the column names (like a space before/after the column name). When I ran the code, it ran fine. However, when I added a space, I was able to replicate this. So, add this line before you plot the figure...

df.columns = df.columns.str.replace(' ', '')
df.plot(x="Ratings", y=["roberta_neg", "roberta_neu","roberta_pos"], kind="bar")

Plot created

enter image description here

Related