Python数据框,比较两列的值

问题描述 投票:0回答:2

scoreA是学生A的分数,scoreB是学生B的分数。我尝试比较他们的分数。

我如何比较分数A和分数B?可以在哪里做?

python
2个回答
0
投票

您可以尝试

df["winner"] = df.apply(lambda x: x[0] if x[2] > x[3] else x[1], axis=1)

0
投票

检查它

import pandas as pd

df=pd.read_csv('score.csv',delimiter=',')
df["STUDENT_WINNER"] = df.apply(lambda x: x[0] if x[2] > x[3] else ( 'EQUAL' if x[2] == x[3] else x[1]), axis=1)
print(df.head())
© www.soinside.com 2019 - 2024. All rights reserved.