Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I'm trying plot a df, but i always find that error: KeyError: 'value'
I already tried define the value as .set_index(''), also tried for other columns, tried define xlabel and ylabel before try the plot but i never had a different result from this.
My idea is 'years' be the xlabel, and the ylabel be the values of each other column.
df = pd.read_csv(r'C:\Users\Gustavo\Desktop\Industrial\dados_agregados1.csv', sep = ';', header = 0)
df2 = df.T
df2.head(1)
print(type(df2))
df2.plot(x = 'years')
i had less than 10 reputations, so, i cant post here the image.
Thanks for all
enter image description here
–
The problem that can be seen in the image is that you mix (due to transpose) column names with actual values (meaning, the row starting with "Years" is seen as data instead of the header).
Solution would be to do something like (full example select only what you require):
import pandas as pd
df = pd.DataFrame(data= {'col1':["CompanyABC", 1000, 1200] , 'col2': ["CompanyXYZ", 333,222]}, index=["Years", 2002, 2003])
# From here would apply to you
df.columns = df.iloc[0]
df_new = df.drop('Years')
df_new.plot.bar()
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.