简体   繁体   English

Keyerror:“[[x,y,z]]都不在[index]中”

[英]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? 我对分析的pandas / python方面很新,所以如果你能立刻看到答案,请告诉我你是如何找到的? 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_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. 如果我需要发布更多代码,请告诉我。

Most likely a typo somewhere is messing up your index keys, or you are losing some during your groupby().sum() operations. 很可能某个地方的拼写错误搞乱你的索引键,或者你在groupby()。sum()操作中丢失了一些。

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'. 一般来说,您应该使用Pandas的“reindex”函数对索引进行重新排序,它将为缺少的键创建新索引并用“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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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