2013-01-23 42 views
3

我正在嘗試將pandas DataFrame和Series寫入xlwt Worksheet對象。一切順利,除非我嘗試寫入numpy.int64數據,在這種情況下xlwt喘氣。在我的數據和單級索引中將int64更改爲float64非常簡單,但是如何爲MultiIndexes做到這一點?將Int64索引更改爲索引並將dtype = int64更改爲dtype = object

t = pd.DataFrame(np.array(np.mat('0 1 0 1; 1 0 2 3; 1 1 2 4'))) 
arrays = [[1,2,3,4],[5,6,7,8]] 
tuples = zip(*arrays) 
index = pd.MultiIndex.from_tuples(tuples, names=['First','Second']) 
t.columns = index 
wb = xlwt.Workbook() 
ws_1 = wb.add_sheet('simple index', cell_overwrite_ok=True) 

In [137]: t 
Out[137]: 
First 1 2 3 4 
Second 5 6 7 8 
0  0 1 0 1 
1  1 0 2 3 
2  1 1 2 4 

In [157]: t.ix[0][1][5] 
Out[157]: 0 

In [158]: ws_1.row(0).write(0, t.ix[0][1][5]) 
------------------------------------------------------------ 
Traceback (most recent call last): 
    File "<ipython console>", line 1, in <module> 
    File "C:\Python27\lib\site-packages\xlwt\Row.py", line 259, in write 
    raise Exception("Unexpected data type %r" % type(label)) 
Exception: Unexpected data type <type 'numpy.int64'> 

In [159]: t.dtypes 
Out[159]: 
First Second 
1  5   int64 
2  6   int64 
3  7   int64 
4  8   int64 

In [160]: idx = t.dtypes[t.dtypes == np.int64].index 

In [161]: idx 
Out[161]: 
MultiIndex 
[(1, 5), (2, 6), (3, 7), (4, 8)] 

In [163]: for i in idx: 
    .....:    t[i] = t[i].astype(np.float64) 
    .....: 

In [164]: t.dtypes 
Out[164]: 
First Second 
1  5   float64 
2  6   float64 
3  7   float64 
4  8   float64 

In [165]: ws_1.row(0).write(0, t.ix[0][1][5]) 

In [167]: t.columns.levels 
Out[167]: [Int64Index([1, 2, 3, 4], dtype=int64), Int64Index([5, 6, 7, 8], dtype=int64)] 

In [168]: t.columns 
Out[168]: 
MultiIndex 
[(1, 5), (2, 6), (3, 7), (4, 8)] 

In [169]: t.columns[0][0] 
Out[169]: 1 

In [170]: ws_1.row(0).write(0, t.columns[0][0]) 
------------------------------------------------------------ 
Traceback (most recent call last): 
    File "<ipython console>", line 1, in <module> 
    File "C:\Python27\lib\site-packages\xlwt\Row.py", line 259, in write 
    raise Exception("Unexpected data type %r" % type(label)) 
Exception: Unexpected data type <type 'numpy.int64'> 
+0

我認爲對付multiindexes目前最好的辦法是使用'to_csv'和進口,['to_excel'](http://stackoverflow.com/questions/14225676/save-list- of-dataframes-to-multisheet-excel-spreadsheet)工作,但刪除列級別名稱/索引級別名稱。 –

+0

@AndyHayden,我的意圖是將這些代碼包含在一個可以處理用戶定義的xlwt樣式的類中,to_excel不允許並且to_csv不支持。我還打算按預期處理色譜柱級別。見[我的博客](http://informedguess.wordpress.com/2013/01/13/pandas-to-excel-with-styles/)。 – dmvianna

+1

'xlwt'喘氣因爲'isinstance(numpy.int64(1234),int)'返回'False'。因爲'isinstance(numpy.int32(1234),int)'返回'True'和'isinstance(numpy.float64(1234),float [numpy.int32] )'returns' True' 請參閱https://github.com/python-excel/xlwt/issues/15 –

回答

-1
for i in range(len(t.columns.levels)): 
    if t.columns.levels[i].dtype == np.int64: 
     t.columns.levels[i] = t.columns.levels[i].astype(np.float64) 
+1

這將拋出一個錯誤:'TypeError:'FrozenList'不支持可變操作。' – denfromufa

+1

正確選項在這裏:http://stackoverflow.com/a/26629643/2230844 – denfromufa

相關問題