Search code examples
pythonpython-3.xpandasdataframekeyerror

Removing Row Indices and Reverting them to a Column


I have a pandas dataframe that looks like the following.

enter image description here

I would like to create a bar chart; however, I can't because LotType isn't seen as a column variable name but rather a row header. How can I make LotType just like the other columns? I'm sure this is an easy fix, but I haven't been able to find the answer to it.

bigdf.plot.bar(x='LotType', y='Ratio2015', figsize=(20, 3), color='maroon')

For reference, here is the error I get which I kind of explained above:

KeyError                                  Traceback (most recent call last)
...
KeyError: 'LotType'

Solution

  • Take a look to reset_index method:

    https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.reset_index.html

    df.reset_index() will create a new index column with just numbers, and your LotType column will be saved as a normal column.