如何在不多次选择同一列和行的情况下选择熊猫数据框上的最高值

我有一个pandas看起来像这样的数据框:


           0           1           2           3

0   0.371292    0.198658    0.178688    0.164981

1   0.262219    0.461267    0.447531    0.194239

2   0.412508    0.105518    0.254549    0.471136

我想选择n较大的数字,n = min(len(df), len(df.columns))连同行名和列名。条件是所有n数字必须row 彼此 column不同。


在上面的例子中,数字[0.471136, 0.461267, 0.371292]应该连同它们各自的一起选择(row, column),所以选择 over the event though is bigger than 的[(2,3), (1,1), (0,0)] 原因是因为之前已经使用过 (for )0.3712920.4475310.4475310.412508row 10.461267


有这样做的pythonic方式吗?


PIPIONE
浏览 79回答 1
1回答

守着星空守着你

这是一个解决方案,可确保您不会从同一行或同一列中选择值:n = min(len(df), len(df.columns))for i in range(n):    t = df.reset_index().melt(id_vars="index")    max_cell = t.iloc[t.value.idxmax()]    row = max_cell["index"]    col = max_cell["variable"]    print(f"max cell is {max_cell}")    df.drop(row, axis=0, inplace = True)    df.drop(col, axis=1, inplace = True)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python