python - 查找多列总和最大的行

标签 python pandas

我用代码创建了DataFrame

import pandas as pd

data = [
    ["id_1",6,7,9],
    ["id_2",9,7,1],
    ["id_3",6,7,10],
    ["id_4",9,5,10]
]
df = pd.DataFrame(data, columns = ['Student Id', 'Math', 'Physical', 'Chemistry'])

我们如何找到 max(Math + Physical + Chemistry), result = id_4

提前致谢!

最佳答案

您可以将 Student Id 列设置为索引,然后计算最大和。

df = df.set_index('Student Id')
print(df.sum(axis=1).idxmax())

关于python - 查找多列总和最大的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58901626/

相关文章:

Python 3 和 GTK3 : crossplatform tray icon

python - 如何最大限度地减少运行时间 : Value Replacement Iterations in Pandas

python - 如何加快 Pandas rolling_sum() 的速度?

python - sqlalchemy - 如何复制深复制条目及其所有外部关系

python - 使用 pythonbrew 编译 Python 3.2 和 2.7 时出现问题

python - 集合.deque : why q[9999] is faster than q[-1]?

Python:调用继承的父类方法失败

python - 根据一列中的值替换整个数据框中的空值

python - Pandas between_time bool 值

python - Tensorflow中如何使用多个汇总集合?