我不知道此错误来自何处。它表示索引中没有任何值。但是我仔细检查了拼写,语法,所有内容都与我的同事用于相似分析的同一张纸完全匹配。
我对分析的pandas / python方面很陌生,所以如果您能立即看到答案,可以告诉我如何找到它吗?我想学习。谢谢!
当我注释掉我在这里显示的最后一行时,一切都会运行
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')
我希望按照列出的顺序排列“存储桶”
再次,我是新手,我知道这可能只是我的知识空白,我们将不胜感激。
让我知道是否需要发布更多代码。
答案 0 :(得分:0)
很可能某个地方的错字弄乱了您的索引键,或者在groupby()。sum()操作期间丢失了一些错字。
尝试:
missing_keys = [key for key in bucket_order if key not in ss_eventtotals_plot.index]
missing_keys
找出丢失的键。
通常,您应该使用Pandas的“重新索引”功能对索引进行重新排序,它将为丢失的键创建新索引,并用“ NaN”填充它们。所以最后一段看起来是:
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')