从pandas中的多索引列中选择列

bcs8qyzn  于 2023-04-28  发布在  其他
关注(0)|答案(1)|浏览(133)

我有一个dataframe,我需要为每个具有更多值的索引选择0级列:我准备了一个例子来澄清。对于每个元素T1,T2,T3,我需要选择多索引列(M1,M2,M3)的级别0,其中它的大小更大。还提供了预期的结果,以及我设法获得结果的方法。

import pandas as pd
import numpy as np
data= [[1,1,1,0,0,0,0,0,0],
[1,1,1,0,0,0,0,0,0],
[1,1,1,0,0,0,0,0,0],
[0,0,0,2,2,2,1,1,1],
[0,0,0,0,0,0,1,1,1],
[0,0,0,2,2,2,1,1,1],
[0,0,0,1,1,1,0,0,0],
[0,0,0,1,1,1,0,0,0],
[0,0,0,1,1,1,0,0,0]]
columns =pd.MultiIndex.from_product([['M1','M2','M3'],['x','y','z']])
index = ['T1','T1','T1','T2','T2','T2','T3','T3','T3']
df = pd.DataFrame(data, index=index, columns = columns ).replace(to_replace = 0, value=np.nan)
df

     M1             M2             M3          
      x    y    z    x    y    z    x    y    z
T1 1.00 1.00 1.00  NaN  NaN  NaN  NaN  NaN  NaN
T1 1.00 1.00 1.00  NaN  NaN  NaN  NaN  NaN  NaN
T1 1.00 1.00 1.00  NaN  NaN  NaN  NaN  NaN  NaN
T2  NaN  NaN  NaN 2.00 2.00 2.00 1.00 1.00 1.00
T2  NaN  NaN  NaN  NaN  NaN  NaN 1.00 1.00 1.00
T2  NaN  NaN  NaN 2.00 2.00 2.00 1.00 1.00 1.00
T3  NaN  NaN  NaN 1.00 1.00 1.00  NaN  NaN  NaN
T3  NaN  NaN  NaN 1.00 1.00 1.00  NaN  NaN  NaN
T3  NaN  NaN  NaN 1.00 1.00 1.00  NaN  NaN  NaN

#Expected RESULT
#     x     y    z
# T1 1.00 1.00 1.00 
# T1 1.00 1.00 1.00 
# T1 1.00 1.00 1.00
# T2 1.00 1.00 1.00
# T2 1.00 1.00 1.00
# T2 1.00 1.00 1.00
# T3 1.00 1.00 1.00 
# T3 1.00 1.00 1.00 
# T3 1.00 1.00 1.00 
# Approach
select = df.stack(level=0).count(axis=1).reset_index().groupby(['level_0','level_1']).sum().unstack(level=1).idxmax(axis=1)

# From this select ( correctly selected M for each T I would like to go to the final RESULTt.
select
Out[52]: 
level_0
T1    (0, M1)
T2    (0, M3)
T3    (0, M2)
r3i60tvu

r3i60tvu1#

IIUC,您可以用途:

#last column in the topmost level
last_col = df.columns.get_level_values(0)[-1] #or `df.columns[-1][0]`

out = (df.stack().ffill(axis=1)[[last_col]]
     .assign(idx=lambda x: x.groupby(level=[0, 1]).cumcount()).reset_index()
     .pivot(index=["level_0", "idx"], columns="level_1", values=last_col)
     .droplevel(1).rename_axis(index=None, columns=None)
)

输出:

print(out)

      x    y    z
T1  1.0  1.0  1.0
T1  1.0  1.0  1.0
T1  1.0  1.0  1.0
T2  1.0  1.0  1.0
T2  1.0  1.0  1.0
T2  1.0  1.0  1.0
T3  1.0  1.0  1.0
T3  1.0  1.0  1.0
T3  1.0  1.0  1.0

相关问题