Pandas error: Writing as Excel with a MultiIndex is not yet implemented

守給你的承諾、 提交于 2021-02-08 06:40:55

问题


I have a pandas data frame that I create as follows:

stats_matrix= #A list containing my data
myindex=['','event 1','event 2','event 3','event 4','event 5','event 6','event 7','event 8','event 9','event 10'] #List used for indexing rows
column_names=['Failed 1st Stage','% Failed 1st Stage','Active 1st Stage','% Active 1st Stage','Failed 2nd Stage','% Failed 2nd Stage','Failed 1st & 2nd','% Failed 1st & 2nd','Active 2nd Stage','% Active 2nd Stage','Total failed','% Total failed ','Total active','% Total active','Giant component','Network diameter','Average clustering','Average degree <k>','Graph length'] #List used for indexing columns

mydf=pd.DataFrame(stats_matrix,index=myindex,columns=column_names)

Then I add an extra row like this:

mydf.columns = pd.MultiIndex.from_tuples(zip(['failed_nodes_1Stage','percent_failed_haz','act_nodes_1Stage','percent_active_haz','failed_nodes_2Stage','percent_failed_conn','failed_nodes_1plus2','percent_failed_1plus2','act_nodes_2Stage','percent_active_conn','total_failed_nodes','percent_total_failed_nodes','total_active_nodes','percent_total_active_nodes','giant_component','network_diam','avg_clustering','average_degree', 'graph_len'], mydf.columns))

but when I want to convert this data frame to an xls, I get an error:

In[30] mydf.to_excel('C:\\Users\\MyName\\Desktop\\My_Results.xls
NotImplementedError                       Traceback (most recent call last)
<ipython-input-30-487e6bf667c1> in <module>()
----> 1 mydf.to_excel('C:\\Users\\MyName\\Desktop\\My_Results.xls')

C:\Users\MyName\AppData\Local\Enthought\Canopy32\User\lib\site-packages\pandas\core\frame.pyc in to_excel(self, excel_writer, sheet_name, na_rep, float_format, columns, header, index, index_label, startrow, startcol, engine, merge_cells, encoding, inf_rep)
   1250         from pandas.io.excel import ExcelWriter
   1251         if self.columns.nlevels > 1:
-> 1252             raise NotImplementedError("Writing as Excel with a MultiIndex is "
   1253                                       "not yet implemented.")
   1254 

NotImplementedError: Writing as Excel with a MultiIndex is not yet implemented.

My question: How can I successfully print my data frame with the extra row to an xls file?

来源:https://stackoverflow.com/questions/33633858/pandas-error-writing-as-excel-with-a-multiindex-is-not-yet-implemented

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!