如何使用 pandas 库删除 Excel 文件中的行

How to delete lines in an Excel file using the pandas library

这是我的代码

def delete_teach():
    df = pd.read_excel('bd1.xlsx', sheet_name='teachers')
    print("Enter the name of the teacher you want to remove:")
    print(df['Teachers'])
    delete_teach_vyb=input("Enter: ")
    print("You are about to delete ", delete_teach_vyb)
    return df.query("Teachers != @delete_teach_vyb")
    df.save()

输入文件后数据没有被删除

您需要将 df 写回 excel 文件。使用 df.to_excel:

def delete_teach():
    df = pd.read_excel('bd1.xlsx', sheet_name='teachers')
    print("Enter the name of the teacher you want to remove:")
    print(df['Teachers'])
    delete_teach_vyb=input("Enter: ")
    print("You are about to delete ", delete_teach_vyb)
    df = df.query("Teachers != @delete_teach_vyb")
    df.to_excel('bd1.xlsx', sheet_name='teachers')

此外,df.save 已弃用。 df.to_pickle是正确的方法。