我有三个这样的数据帧:
groups:
0 1 2 3
0 e f g h
1 e f s h
2 q f g r
3 e r g b
items:
0 1 2 3
0 ret sef sdf fhs
1 hnf cbv awd øjg
2 gry bcr vbd dgf
3 xfh sjd krt qeb
weights:
0 1 2 3
0 241 234 343 34
1 23 276 334 42
2 561 256 35 345
3 234 654 754 234
我想把这些放在一起,得到一个多索引,第一个索引是组,第二个索引是项,值是权重。例如,在0,0中我有“e”(第一个索引)、“ret”(第二个索引)和241(值),或者在2,3中我有“g”、“krt”和754。你知道吗
我怎样才能做到这一点?我不想重复数据集,而是用“熊猫方式”来做。你知道吗
这与问题Combine multiple pandas DataFrames into a multi-index DataFrame不同,因为我希望它看起来像这样:
e ret 241 <-- from 0,0 in each dataframe
hnf 23 <-- from 0,1 in each dataframe
xfh 234 <-- from 0,3 in each dataframe
q gry 561 <-- from 0,2 in each dataframe
等等。。。也就是说,我把数据帧放在彼此的上面,然后创建一个三维数据帧,“查看它们”。你知道吗
您可以取消所有数据帧的堆栈,然后使用
set_index
。我决定sort_values
,这样所有相应的索引值都彼此相邻。你知道吗相关问题 更多 >
编程相关推荐