[英]Function to get row Value of a dataframe using str Index
Sample df:样本df:
Student Marks
Avery 70
Joe 80
John 75
Jordan 90
I want to use a function as below to return marks when a student name is passed.当学生姓名通过时,我想使用如下 function 来返回分数。
def get_marks(student):
return *something*
Expected Output: get_marks('Joe') ==> 80预期 Output:get_marks('Joe') ==> 80
I think the following might work.我认为以下可能有效。
def get_marks(student):
p = df.index[df['Student'] == student].tolist()
p = p[0]
return df['Marks'][p]
What I have done is I have first to get the index of row of the Student and then simply return the Marks for the same index.我所做的是首先获取学生行的索引,然后简单地返回相同索引的标记。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.