Search code examples
pythonpython-3.xpandasjupyter-notebookkeyerror

Keyerror: "none of [[x,y,z]] are in the [index]"


I don't understand where this error is coming from. it says that none of the values are in the index. But I have double checked spellings, syntax, everything is matching to the exact same sheet my colleague is using for a similar analysis.

I am very new to the pandas/python side of analytics, so please if you can see the answer right away could you tell me how you found it? I want to learn. Thanks!

everything runs when I comment out the last line I've shown here ss_eventtotals_plot.loc[bucket_order].plot(kind='bar')

ss=pd.read_csv('cars_cars_saved_search_data.csv',parse_dates=[0])
ss_eventtotals=ss
ss_eventtotals.loc[ss_eventtotals.index.isin(['saved search|open|srp','saved search|open|nav',
                                              'save search|create start']),'Bucket'] = 'create start' 
ss_eventtotals.loc[ss_eventtotals.index.isin(['saved search|create|srp','saved search|create|nav',
                                              'save search|create success']),'Bucket'] = 'create complete'
ss_eventtotals.loc[ss_eventtotals.index.isin(['save search|create cancel']),'Bucket'] = 'create cancel - Web' 
ss_eventtotals.loc[ss_eventtotals.index.isin(['saved search|delete']),'Bucket'] = 'delete start' 
ss_eventtotals.loc[ss_eventtotals.index.isin(['saved search|delete cancel']),'Bucket'] = 'delete cancel'
ss_eventtotals.loc[ss_eventtotals.index.isin(['saved search|delete success',
                                              'save search|delete success']),'Bucket'] = 'delete complete'
ss_eventtotals.loc[ss_eventtotals.index.isin(['saved search|edit','save search|edit start']),'Bucket'] = 'edit start'
ss_eventtotals.loc[ss_eventtotals.index.isin(['saved search|edit cancel',
                                              'save search|edit cancel']),'Bucket'] = 'edit cancel'
ss_eventtotals.loc[ss_eventtotals.index.isin(['saved search|edit save',
                                              'save search|edit success']),'Bucket'] = 'edit complete'
ss_eventtotals.loc[ss_eventtotals.index.isin(['saved search|view']),'Bucket'] = 'view - App' 
ss_eventtotals.loc[ss_eventtotals.index.isin(['save search|add cancel']),'Bucket'] = 'add cancel - Web' 
ss_eventtotals.loc[ss_eventtotals.index.isin(['saved search|run search']),'Bucket'] = 'run search - App' 
ss_eventtotals.loc[ss_eventtotals.index.isin(['saved search|view']),'Bucket'] = 'search view - App' 
ss_eventtotals2=ss_eventtotals.groupby('Bucket')
ss_eventtotals2=ss_eventtotals2.sum()
ss_eventtotals_plot=ss_eventtotals2.loc[ss_eventtotals2.index.isin(['create start','create complete','delete start','delete complete','edit start','edit complete','view - App','run search - App'])]
bucket_order = ['create start','create complete','delete start','delete complete','edit start','edit complete','view - App','run search - App']
ss_eventtotals_plot.loc[bucket_order].plot(kind='bar')

I expected the 'buckets' to be plotted in the order I have them listed

Again, I'm new and I know this is just probably a knowledge gap I have, any assistance is greatly appreciated.

Let me know if I need to post more of my code.


Solution

  • Most likely a typo somewhere is messing up your index keys, or you are losing some during your groupby().sum() operations.

    Try :

    missing_keys = [key for key in bucket_order if key not in ss_eventtotals_plot.index]
    missing_keys
    

    To figure out which keys are missing.

    In general you should use Pandas 'reindex' function to reorder the index, it will create new indexes for missing keys and fill them with 'NaN'. So the last paragraph would look :

    bucket_order = ['create start','create complete','delete start','delete complete','edit start','edit complete','view - App','run search - App']
    ss_eventtotals_plot=ss_eventtotals2.reindex([bucket_order])
    ss_eventtotals_plot.loc[bucket_order].plot(kind='bar')