使用分层列创建DataFrame [英] Creating DataFrame with Hierarchical Columns
问题描述
使用分层列创建DataFrame
的最简单方法是什么?
What is the easiest way to create a DataFrame
with hierarchical columns?
我当前正在使用以下命令从名称字典-> Series
创建一个DataFrame:
I am currently creating a DataFrame from a dict of names -> Series
using:
df = pd.DataFrame(data=serieses)
我想使用相同的列名,但在列上添加一个更高级别的层次结构.我暂时希望其他级别的列具有相同的值,例如估算".
I would like to use the same columns names but add an additional level of hierarchy on the columns. For the time being I want the additional level to have the same value for columns, let's say "Estimates".
我正在尝试以下操作,但这似乎不起作用:
I am trying the following but that does not seem to work:
pd.DataFrame(data=serieses,columns=pd.MultiIndex.from_tuples([(x, "Estimates") for x in serieses.keys()]))
我得到的是一个带有所有NaN的DataFrame.
All I get is a DataFrame with all NaNs.
例如,我要寻找的大致是:
For example, what I am looking for is roughly:
l1 Estimates
l2 one two one two one two one two
r1 1 2 3 4 5 6 7 8
r2 1.1 2 3 4 5 6 71 8.2
其中l1和l2是MultiIndex的标签
where l1 and l2 are the labels for the MultiIndex
推荐答案
这似乎有效:
import pandas as pd
data = {'a': [1,2,3,4], 'b': [10,20,30,40],'c': [100,200,300,400]}
df = pd.concat({"Estimates": pd.DataFrame(data)}, axis=1, names=["l1", "l2"])
l1 Estimates
l2 a b c
0 1 10 100
1 2 20 200
2 3 30 300
3 4 40 400
这篇关于使用分层列创建DataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!