我有一个DataFrame:
d = pd.DataFrame({'i1': ['A', 'B', 'C', 'D', 'E'],
'i2': ['I', 'II', 'III', 'IV', 'V'],
'val': ["lol1", "lol2", "lol3", "lol4", "lol5"]}).set_index(["i1", "i2"])
val
i1 i2
A I lol1
B II lol2
C III lol3
D IV lol4
E V lol5
我需要使用熊猫API从值i3
添加新的索引级别[5, 10, 15]
,
val
i1 i2 i3
A I 5 lol1
10 lol1
15 lol1
B II 5 lol2
10 lol2
15 lol2
C III 5 lol3
10 lol3
15 lol3
D IV 5 lol4
10 lol4
15 lol4
E V 5 lol5
10 lol5
15 lol5
我的尝试(丑陋):
d = np.repeat(d.reset_index().values, 3, 0)
i3 = [5, 10, 15]
r2 = np.tile(i3, 5)
r = np.concatenate([d, r2.reshape(-1, 1)], 1)
d = pd.DataFrame(r, columns=["i1", "i2", "val", "i3"])
d = d.set_index(["i1", "i2", "i3"])
此外,我一直在寻找pd.MultiIndex.from_product
,但是无论我做什么,它都会从i1
和i2
组合而成。
答案 0 :(得分:3)
根据MultiIndex
中的值创建元组列表,然后将DataFrame.reindex
与MultiIndex.from_tuples
结合使用:
vals = [5, 10, 15]
tups = [x + (i,) for x in d.index for i in vals]
d = d.reindex(pd.MultiIndex.from_tuples(tups, names=['i1','i2','i3']))
print (d)
val
i1 i2 i3
A I 5 lol1
10 lol1
15 lol1
B II 5 lol2
10 lol2
15 lol2
C III 5 lol3
10 lol3
15 lol3
D IV 5 lol4
10 lol4
15 lol4
E V 5 lol5
10 lol5
15 lol5