Python 如何使用pandas从csv文件中删除一行?

Python 如何使用pandas从csv文件中删除一行?,python,pandas,Python,Pandas,我正在尝试使用pandas从csv文件中删除特定行。这就是我到目前为止所做的: def delete_from_file(): student_name = input("What is the name of the student? ") df = pd.read_csv('students.csv', names = ['name', 'phone number', 'class time', 'duration'], index_col=0) df.drop(st

我正在尝试使用pandas从csv文件中删除特定行。这就是我到目前为止所做的:

def delete_from_file():
    student_name = input("What is the name of the student? ")
    df = pd.read_csv('students.csv', names = ['name', 'phone number', 'class time', 'duration'], index_col=0)
    df.drop(student_name)
    print(df)
这是csv文件中的采样线的外观:

卡尔·亨德森,900-122-818,下午12:15,30分钟


我希望能够为学生的名字写上“Carl Henderson”,并基本上删除整行。

将函数改为

def delete_from_file(student_name):
    df = pd.read_csv('students.csv', names = ['name', 'phone number', 'class time', 'duration']).set_index('name')
    df=df.drop(student_name)
    return df

将您的函数更改为

def delete_from_file(student_name):
    df = pd.read_csv('students.csv', names = ['name', 'phone number', 'class time', 'duration']).set_index('name')
    df=df.drop(student_name)
    return df

我仍然得到相同的错误,即“['student_name']未在axis中找到”。知道是什么原因吗?@rod.hoda不是“student\u name”应该是student\u name我仍然得到相同的错误,即“['student\u name']未在axis中找到”。知道是什么原因吗?@rod.hoda不是“学生名”应该是学生名