<p>以下是我的出发点(与您的代码相同,不同的随机值):</p>
<pre><code> 0 1 2 3
A1 B1 C1 3 4 1 6
C2 6 3 4 5
B2 C1 8 3 5 1
C2 8 5 1 6
A2 B1 C1 8 7 0 6
C2 5 1 4 7
B2 C1 3 1 8 5
C2 7 1 7 8
</code></pre>
<hr/>
<pre><code>df[0] = df.groupby(level=[0,1])[0].transform(max)
0 1 2 3
A1 B1 C1 6 4 1 6
C2 6 3 4 5
B2 C1 8 3 5 1
C2 8 5 1 6
A2 B1 C1 8 7 0 6
C2 8 1 4 7
B2 C1 7 1 8 5
C2 7 1 7 8
</code></pre>
<p>在第一个级别中,我找不到直接检查“1”的方法,所以我只是将它转换为带有<code>reset_index</code>的colunn,然后对它使用string方法就相当容易了。你知道吗</p>
<pre><code>df['one'] = df.reset_index().level_0.str.contains('1').values
df['nth'] = np.where( df.one, df[2], df[3] )
0 1 2 3 one nth
A1 B1 C1 6 4 1 6 True 1
C2 6 3 4 5 True 4
B2 C1 8 3 5 1 True 5
C2 8 5 1 6 True 1
A2 B1 C1 8 7 0 6 False 6
C2 8 1 4 7 False 7
B2 C1 7 1 8 5 False 5
C2 7 1 7 8 False 8
</code></pre>
<p>现在把事情清理干净(有些事情可以早点做,但我认为等到最后再把它们结合起来会更清楚):</p>
<pre><code>df.iloc[0::2,[0,-1]].reset_index(level=2,drop=True).rename(columns={0:'max'})
max nth
A1 B1 6 1
B2 8 5
A2 B1 8 6
B2 7 5
</code></pre>
<p>我不确定你是否也在问concat,但很简单:</p>
<pre><code>pd.concat( [df1,df2], axis=1)
</code></pre>