pandas 创建一个带有 MultiIndex 的 DataFrame
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38901145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create a DataFrame with a MultiIndex
提问by stormtrooper12
I would like to make my DataFrame like the one below and export it to excel. I have all the data available for all the '-' that I have put. I want to know what data structure to pass to pd.Dataframe() to make a table like this.
我想让我的 DataFrame 像下面这样,并将其导出到 excel。我有所有可用的数据,可用于我输入的所有“-”。我想知道将什么数据结构传递给 pd.Dataframe() 来制作这样的表格。
Would like to know how pandas read these data structures to form a DataFrame.
想知道 pandas 如何读取这些数据结构以形成 DataFrame。
回答by piRSquared
idx = pd.MultiIndex.from_product([['Zara', 'LV', 'Roots'],
['Orders', 'GMV', 'AOV']],
names=['Brand', 'Metric'])
col = ['Yesterday', 'Yesterday-1', 'Yesterday-7', 'Thirty day average']
df = pd.DataFrame('-', idx, col)
df
Jupyter screen shot
Jupyter 屏幕截图
df.to_excel('test.xlsx')
Mac Numbers screen shot
Mac Numbers 屏幕截图