我正在寻找从笛卡尔乘积在 pandas 中创建一个 MultiIndex,其中一个级别是“特殊的”并且将与任意数量的附加级别相关联,我想在与特殊级别相同。最终结果证明比描述容易得多。
下面的代码显示了我想基于 id 和 loc 的笛卡尔积创建一个 MultiIndex 但有 color 和shape 以与“id”相同的方式展开。示例中显示了执行此操作的两种不同方法。对于这种人为设计的情况,这些是足够的解决方案,但对于我的 DataFrame 将有超过 1000 万行的实际用例,这些方法都不能满足我的性能要求。创建此类 MultiIndex 的最佳方法是什么?
import pandas as pd
import numpy as np
id = np.asarray([1,2,3,4,5])
color= np.asarray(['red','blue','green','orange','purple'])
shape = np.asarray(['square','circle','triangle','rectangle','oval'])
loc = np.asarray(['CA','OR'])
idx = pd.MultiIndex.from_product([id,loc], names=['ID','LOC'])
data = np.ravel(np.random.rand(5,2))
# Approach 1
df1 = pd.DataFrame(data, index=idx)
df1['color'] = color[idx.labels[0]]
df1['shape'] = shape[idx.labels[0]]
df1.set_index(['color','shape'],append=True,inplace=True)
print(df1)
# Approach 2
idx2 = pd.MultiIndex.from_arrays([id[idx.labels[0]],loc[idx.labels[1]],color[idx.labels[0]],shape[idx.labels[0]]],names=['ID','LOC','color','shape'])
df2 = pd.DataFrame(data, index=idx2)
print(df2)
请您参考如下方法:
pd.MultiIndex.from_tuples v1
midx = pd.MultiIndex.from_tuples(
[(id[i], l, color[i], shape[i])
for i in range(len(id)) for l in loc],
names=['ID', 'LOC', 'color', 'shape']
)
df3 = pd.DataFrame(data, midx)
df3
0
ID LOC color shape
1 CA red square 0.583714
OR red square 0.038577
2 CA blue circle 0.879020
OR blue circle 0.542611
3 CA green triangle 0.185523
OR green triangle 0.289909
4 CA orange rectangle 0.788596
OR orange rectangle 0.915843
5 CA purple oval 0.701603
OR purple oval 0.726648
pd.MultiIndex.from_tuples v2
i, j = np.indices((len(id), len(loc)))
a = np.column_stack([
np.column_stack([id, color, shape])[i.ravel()],
loc[j.ravel()]
])[:, [0, -1, 1, 2]]
midx = pd.MultiIndex.from_arrays(a.tolist(), names=['ID', 'LOC', 'color', 'shape'])
df4 = pd.DataFrame(data, midx)
df4
0
ID LOC color shape
1 CA red square 0.583714
OR red square 0.038577
2 CA blue circle 0.879020
OR blue circle 0.542611
3 CA green triangle 0.185523
OR green triangle 0.289909
4 CA orange rectangle 0.788596
OR orange rectangle 0.915843
5 CA purple oval 0.701603
OR purple oval 0.726648
