Python Columns参数不存在';导出excel文件时无法工作(熊猫)

Python Columns参数不存在';导出excel文件时无法工作(熊猫),python,excel,pandas,parameters,Python,Excel,Pandas,Parameters,第一张表包含并且应该包含所有原始列,而第二张表仅包含我提到的那些列。但是,最后一个仍然包含所有原始列,仍然显示另外三个列,就好像我从未输入columns=参数一样 更新:此格式也不起作用: url = 'https://data.cityofnewyork.us/api/views/25th-nujf/rows.csv' baby_names = pd.read_csv(url) girls = baby_names[baby_names['Gender'] == 'FEMALE'] boys

第一张表包含并且应该包含所有原始列,而第二张表仅包含我提到的那些列。但是,最后一个仍然包含所有原始列,仍然显示另外三个列,就好像我从未输入
columns=
参数一样

更新:此格式也不起作用:

url = 'https://data.cityofnewyork.us/api/views/25th-nujf/rows.csv'
baby_names = pd.read_csv(url)

girls = baby_names[baby_names['Gender'] == 'FEMALE']
boys = baby_names[baby_names['Gender'] == 'MALE']

excel_file = pd.ExcelWriter('Baby_Names.xlsx')

girls.to_excel(excel_file, sheet_name='Girls', index=False)
boys.to_excel(excel_file, sheet_name='Boys', index=False, columns=['Year of Birth', 'Gender', 'Ethnicity'])

excel_file.save()
提供了使用ExcelWriter编写多个工作表的示例,如下所示:

with pd.ExcelWriter('Baby_Names.xlsx') as excel_file:
    girls.to_excel(excel_file, sheet_name='Girls', index=False)
    boys.to_excel(excel_file, sheet_name='Boys', index=False, columns=['Year of Birth', 'Gender', 'Ethnicity'])
    excel_file.save()
也许可以尝试使用此单一文件上下文编写两个工作表

with pd.ExcelWriter('output.xlsx') as writer:  
    df1.to_excel(writer, sheet_name='Sheet_name_1')
    df2.to_excel(writer, sheet_name='Sheet_name_2')
 excel_file = pd.ExcelWriter('Baby_Names.xlsx')
 with excel_file as writer:  
    girls.to_excel(excel_file, sheet_name='Girls', index=False)
    boys.to_excel(excel_file, sheet_name='Boys', index=False, columns=['Year of Birth', 'Gender', 'Ethnicity'])