tryingtobeastoic
tryingtobeastoic

Reputation: 195

Why can't i use pandas.DataFrame.plot.bar() properly?

This is my dataframe:

Month    January   February      March      April        May       June  \
Year                                                                      
2016         NaN        NaN        NaN        NaN    97162.0   415627.0   
2017   1016340.0   871166.0   910442.0   926362.0  1061573.0  1307325.0   
2018   1815983.0  1839012.0  1943507.0  1870525.0  1753449.0  2103510.0   
2019   3163752.0  2967114.0  2827649.0  2681053.0  2834637.0  2532198.0   

Month       July     August  September    October   November   December  
Year                                                                     
2016    675071.0   962525.0  1244306.0   849348.0  1213459.0   862805.0  
2017   2040012.0  1479086.0  1421304.0  1470600.0  1731047.0  1501038.0  
2018   1971323.0  1947780.0  1978252.0  3118588.0  2360650.0  2481472.0  
2019   2917097.0  2978802.0  2918065.0  2701650.0  2004330.0   301467.0  

When I run the code below:

df.plot.bar(x = df.index, y = "January")

It gives me the error:

KeyError: "None of [Int64Index([2016, 2017, 2018, 2019], dtype='int64', name='Year')] are in the [columns]"

Why is this happening?

These might help you answer the question: Input:

print(df.columns)
print(df.index)
print(type(df.columns))
print(type(df.index))

Output:

Index(['January', 'February', 'March', 'April', 'May', 'June', 'July',
       'August', 'September', 'October', 'November', 'December'],
      dtype='object', name='Month')
Int64Index([2016, 2017, 2018, 2019], dtype='int64', name='Year')
<class 'pandas.core.indexes.base.Index'>
<class 'pandas.core.indexes.numeric.Int64Index'>

Upvotes: 1

Views: 235

Answers (1)

jezrael
jezrael

Reputation: 863166

You can check DataFrame.plot.bar:

x - label or position, optional
Allows plotting of one column versus another. If not specified, the index of the DataFrame is used.

So need omit x = df.index:

df.plot.bar(y = "January")

Upvotes: 1

Related Questions