Python 如何在无循环的熊猫系列中通过索引搜索数据

Python 如何在无循环的熊猫系列中通过索引搜索数据,python,pandas,Python,Pandas,我需要编写一个接受3个参数(series、firstname、lastname)的函数。系列是pd系列,其中索引为学生姓名的成绩。函数应该输出给定学生的成绩 例如:函数((pd.Series([A,B],index=['Ann Smith','John Doe]),'John','Doe']。输出应该是'B' 使用一个已知的名称,这将只是切片,如Series['johndoe]-->B' 但是如何在函数中实现这一点呢?循环和if是被禁止的。类似的东西 s = pd.Series(['A', 'B

我需要编写一个接受3个参数(series、firstname、lastname)的函数。系列是pd系列,其中索引为学生姓名的成绩。函数应该输出给定学生的成绩

例如:
函数((pd.Series([A,B],index=['Ann Smith','John Doe]),'John','Doe']
。输出应该是'B'

使用一个已知的名称,这将只是切片,如
Series['johndoe]-->B'

但是如何在函数中实现这一点呢?循环和if是被禁止的。

类似的东西

s = pd.Series(['A', 'B'], index = ['Ann Smith', 'John Doe'])

def get_grade(series, first_name, last_name):
    name = f"{first_name} {last_name}"
    return series[name]

get_grade(s, 'John', 'Doe')
像这样的

s = pd.Series(['A', 'B'], index = ['Ann Smith', 'John Doe'])

def get_grade(series, first_name, last_name):
    name = f"{first_name} {last_name}"
    return series[name]

get_grade(s, 'John', 'Doe')

是的,谢谢!刚刚写了一个类似的名字,它起作用了(名字='firstname'+''+'lastname')是的,谢谢!刚刚写了一个类似的名字,它起作用了(名字='firstname'+''+'lastname'))