[英]Merge multiindex with multiple column levels and dataframe
Say I have a multiindex mi
as follows: 假设我有一个多索引
mi
,如下所示:
Serial No. Date
A B A B
0 816292 934609 27/01/17 27/01/17
1 983803 683858 25/01/17 26/01/17
2 596573 493741 27/01/17 28/01/17
3 199203 803515 28/01/17 28/01/17
A and B are two parts such that the multiindex contains information about the serial number and build date of multiple instances of the two parts. A和B是两个部分,因此multiindex包含有关两个部分的多个实例的序列号和构建日期的信息。
I have a dataframe df
containing test information for part A, as follows: 我有一个数据帧
df
其中包含A部分的测试信息,如下所示:
A Test 1 Test 2 Test 3
0 816292 0.934609 0.475035 0.822712
1 983803 0.683858 0.025861 0.691112
2 596573 0.493741 0.397398 0.489101
3 199203 0.803515 0.679537 0.308588
I would like to be able to merge these two and yield something like 我希望能够合并这两个并产生类似
Serial No. Date Tests
A B A B Test 1 Test 2 Test 3
0 816292 934609 27/01/17 27/01/17 0.934609 0.475035 0.822712
1 983803 683858 25/01/17 26/01/17 0.683858 0.025861 0.691112
2 596573 493741 27/01/17 28/01/17 0.493741 0.397398 0.489101
3 199203 803515 28/01/17 28/01/17 0.803515 0.679537 0.308588
My initial attempt was 我最初的尝试是
mi = mi.merge(df,left_on=('Serial No.','A'),right_on='A',how='inner')
but that yields ValueError: len(right_on) must equal len(left_on)
. 但这会产生
ValueError: len(right_on) must equal len(left_on)
。 I have tried adding an additional column index 'Tests'
to df
and then doing 我尝试向
df
添加一个额外的列索引'Tests'
,然后执行
mi = mi.merge(df,left_on=('Serial No.','A'),right_on=('Tests','A'),how='inner')
but that yields KeyError: 'A'
但这会产生
KeyError: 'A'
The easiest way is to fix df
's columns to match mi
: 最简单的方法是修复
df
的列以匹配mi
:
In [11]: df
Out[11]:
A Test 1 Test 2 Test 3
0 816292 0.934609 0.475035 0.822712
1 983803 0.683858 0.025861 0.691112
2 596573 0.493741 0.397398 0.489101
3 199203 0.803515 0.679537 0.308588
In [12]: df.columns = pd.MultiIndex.from_arrays([["Serial No.", "Test", "Test", "Test"], df.columns])
In [13]: df
Out[13]:
Serial No. Test
A Test 1 Test 2 Test 3
0 816292 0.934609 0.475035 0.822712
1 983803 0.683858 0.025861 0.691112
2 596573 0.493741 0.397398 0.489101
3 199203 0.803515 0.679537 0.308588
Then a merge will "just work": 然后合并将“正常工作”:
In [14]: df.merge(mi)
Out[14]:
Serial No. Test Serial No. Date
A Test 1 Test 2 Test 3 B A B
0 816292 0.934609 0.475035 0.822712 934609 27/01/17 27/01/17
1 983803 0.683858 0.025861 0.691112 683858 25/01/17 26/01/17
2 596573 0.493741 0.397398 0.489101 493741 27/01/17 28/01/17
3 199203 0.803515 0.679537 0.308588 803515 28/01/17 28/01/17
There's a bunch of ways to create the top level of the MultiIndex, here I just wrote the list: 有多种方法可以创建MultiIndex的顶层,在这里,我只是编写了列表:
["Serial No.", "Test", "Test", "Test"]
by hand... but you can generate that: it's just a list. 手工...但是您可以生成它:它只是一个列表。
mi.set_index(('Serial No.', 'A')).join(
pd.concat([df.set_index('A')], axis=1, keys=['Tests'])
).reset_index()
Serial No. Date Tests
A B A B Test 1 Test 2 Test 3
0 816292 934609 27/01/17 27/01/17 0.934609 0.475035 0.822712
1 983803 683858 25/01/17 26/01/17 0.683858 0.025861 0.691112
2 596573 493741 27/01/17 28/01/17 0.493741 0.397398 0.489101
3 199203 803515 28/01/17 28/01/17 0.803515 0.679537 0.308588
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.