简体   繁体   中英

Function to get row Value of a dataframe using str Index

Sample 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.

def get_marks(student):
    return *something*

Expected 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM