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

Try to format your code by using ```. Place them before and after the code snippet, so that it will monospace it and make it look more readable to other users. Xiddoc Apr 28, 2021 at 20:12

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()
                if i use the transpose i receive another error with expected values was 24, but i only received 15, and i dont i yet receive a Keyerror
– Gustavo Lima
                Apr 28, 2021 at 14:23
        

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.