我用代码创建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)的行,结果= id_4
提前谢谢!
答案 0 :(得分:2)
您可以将“学生ID”列设置为索引,然后计算最大和。
df = df.set_index('Student Id')
print(df.sum(axis=1).idxmax())
答案 1 :(得分:1)
test_result <- tibble::tribble(
~group_name, ~id_name, ~varA, ~varB,
"groupA", "id_1", 1, "a",
"groupA", "id_2", 4, "f",
"groupA", "id_4", 6, "x",
"groupA", "id_4", 6, "h",
"groupB", "id_1", 2, "s",
"groupB", "id_2", 13, "y",
"groupB", "id_4", 14, "t",
"groupC", "id_1", 3, "d",
"groupC", "id_2", 7, "j",
"groupC", "id_4", 9, "l",
)
答案 2 :(得分:1)
您可以尝试以下方法:
df.loc [df [['Math','Physical','Chemistry']]。sum(axis = 1).idxmax(),'Student Id']