简体   繁体   English

Function 使用 str 索引获取 dataframe 的行值

[英]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.

相关问题 如何使用 str.contains function 使用行的索引替换单元格值 - How to replace a cell value using str.contains function using the index of the row 从Pandas DataFrame获取最大值的行索引和列索引 - Get the row index and column index of maximum value from Pandas DataFrame 使用索引值和索引 position 删除 Pandas Dataframe 中的行 - Drop row in a Pandas Dataframe by using index value and index position 根据重复的str值的索引合并数据帧中的str值? - Merging str values in dataframe based on index of a repeating str value? 如何使用 function 中的行索引和单元格值应用于 dataframe? - How to use row index and cell value in function applied to dataframe? 使用索引值将函数应用于特定行 - Apply a function to a specific row using the index value 获取 Dataframe Pandas 中最大值的列和行索引 - Get Column and Row Index for Highest Value in Dataframe Pandas Pandas:获取已知值的索引并将该行复制到另一个 dataframe - Pandas: Get the index of a known value and copy that row to another dataframe Python-有没有一种方法可以使用枚举获取(str,index)而不是(index,str)? - Python - Is there a way to get (str, index) instead of (index, str) using enumerate? 无法使用行索引从python数据框中获取行 - can not get rows from python dataframe using row index
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM