简体   繁体   中英

KeyError: "None of [Index(['Zomba', 'Lilongwe', 'Blantyre', 'Mzuzu'], dtype='object')] are in the [columns]", I am using Python Panda

The following is my dataset and libraries involved:

             import pandas as pd
             import matplotlib.pyplot as plt
             %matplotlib inline

             data={
             'city':['Zomba','Lilongwe','Blantyre','Mzuzu'],
             'rank':[1,4,3,8],
            'region':['south','central','south','north']
            }`

            frame=pd.DataFrame(data,columns=['city','rank','region'])

            myplot=frame.plot(frame['city'], kind='bar', legend=True)

After running the above snippet, I am getting this key error:

KeyError: "None of [Index(['Zomba', 'Lilongwe', 'Blantyre', 'Mzuzu'], dtype='object')] are in the [columns]"

I tried getting my column data with this code: frame.get('city', default=0) and I also tried getting it using frame.iloc[:,0].

and the data is available as shown below: 0 Zomba 1 Lilongwe 2 Blantyre 3 Mzuzu Name: city, dtype: object

But when I try to plot, I am getting the same error.

The last two lines should look like below, you already apply the function plot on your frame, don't need to assign again the series, just the column name.

frame=pd.DataFrame(data)

myplot=frame.plot('city', kind='bar', legend=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM