熊猫据帧与multindex脱颖而出
问题描述:
我试图让这个熊猫据帧与multindex脱颖而出
但我发现了这一点:
即使没有内容,我得到一个垂直多指标。这是我在做什么MWE
from pandas import DataFrame, MultiIndex, ExcelWriter, Series
import numpy.random as rd
FILENAME = 'SampleFile.xlsx'
writer = ExcelWriter(FILENAME, engine='xlsxwriter')
multindex_headers = [['A', 'B', 'C', 'D'],
['A1', 'B2','C3', 'D4']]
index = MultiIndex.from_tuples(list(zip(*multindex_headers)), names=['l1','l2'])
sampleDF = DataFrame(index=index)
sample_info = [rd.randn(4).tolist()]*5
for sample_data in sample_info:
sampleDF = sampleDF.append(DataFrame(sample_data, index=index))
sampleDF.to_excel(writer, sheet_name='case')
writer.save()
编辑:
我已经实现与转置(解决方法),但我想知道是否有一个正式的方法来此问题:
from pandas import DataFrame, MultiIndex, ExcelWriter, Series
import numpy.random as rd
FILENAME = 'SampleFile.xlsx'
writer = ExcelWriter(FILENAME, engine='xlsxwriter')
multindex_headers = [['A', 'B', 'C', 'D'],
['A1', 'B2','C3', 'D4']]
index = MultiIndex.from_tuples(list(zip(*multindex_headers)))
sampleDF = DataFrame(index=index)
sampleDF = sampleDF.transpose()
sample_info = [rd.randn(4).tolist()]*5
for sample_data in sample_info:
sampleDF = sampleDF.append(DataFrame(sample_data, index=index).transpose())
#sampleDF.to_csv('samplecsv.csv')
sampleDF.to_excel(writer, sheet_name='case')
writer.save()
答
from pandas import DataFrame, MultiIndex, ExcelWriter, Series
import numpy.random as rd
FILENAME = 'SampleFile.xlsx'
writer = ExcelWriter(FILENAME, engine='xlsxwriter')
multindex_headers = [['A', 'B', 'C', 'D'],
['A1', 'B2','C3', 'D4']]
index = MultiIndex.from_tuples(list(zip(*multindex_headers)))
sampleDF = DataFrame(index=index)
sampleDF = sampleDF.transpose()
sample_info = [rd.randn(4).tolist()]*5
for sample_data in sample_info:
sampleDF = sampleDF.append(DataFrame(sample_data, index=index).transpose())
#sampleDF.to_csv('samplecsv.csv')
sampleDF.to_excel(writer, sheet_name='case')
writer.save()
从我的编辑拍摄
请在DATAF再看看RAME。这就是我所看到的。 http://imgur.com/vjZYYp9 – atkawa7