为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PIPIONE 2023-03-22 10:46:27
我有一个pandas看起来像这样的数据框:           0           1           2           30   0.371292    0.198658    0.178688    0.1649811   0.262219    0.461267    0.447531    0.1942392   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方式吗?
查看完整描述

1 回答

?
守着星空守着你

TA贡献1799条经验 获得超8个赞

这是一个解决方案,可确保您不会从同一行或同一列中选择值:


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)


查看完整回答
反对 回复 2023-03-22
  • 1 回答
  • 0 关注
  • 53 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信