我一直在使用面板来代替具有多级索引的数据帧,因为对于大型数据集,它们似乎更快。但我现在正在过渡到Midx框架。有了面板,我可以很容易地做到:
import pandas as pd
pan = pd.Panel(np.random.randn(3,5,2),items=['p1','p2','p3'],minor_axis=['a','b'])
然后添加新项目:
^{pr2}$但对于Midx:
cols = [['p1','p2','p3'],['a','b']]
idx = pd.MultiIndex.from_product(cols)
df_midx = pd.DataFrame(np.random.randn(10,6),columns=idx)
这将返回一个错误:
df_midx['p4'] = pd.DataFrame(np.random.randn(10,2),columns=['a','b'])
ValueError: Wrong number of items passed 2, placement implies 1
您可以使用
concat
,因为您正在尝试分配一个数据帧,并且您有多级列,即第1步:使数据帧成为多级列数据帧
或者:
^{pr2}$第2步:合并两个数据帧
相关问题 更多 >
编程相关推荐