pandas写入excel提示FutureWarning: As the xlwt package is no longer maintained

使用Python的pandas库创建了一个DataFrame,设置了'id'为索引,并将其保存到Excel文件。由于xlwt包即将被弃用,pandas警告应使用openpyxl代替。只需将.to_excel()方法中的文件格式改为.xlsx即可解决。

 

import pandas

data=pandas.DataFrame({
    'id':[1,2,3],
    'name':['user','admin','nothing'],
    'age':[18,20,11],
    'city':['北京','上海','深圳']
})
data=data.set_index('id')
print(data)

data.to_excel('1.xls')
print('write done!')

./pandas_test.py:16: FutureWarning: As the xlwt package is no longer maintained, the xlwt engine will be removed in a future version of pandas. This is the only engine in pandas that supports writing in the xls format. Install openpyxl and write to an xlsx file instead. You can set the option io.excel.xls.writer to 'xlwt' to silence this warning. While this option is deprecated and will also raise a warning, it can be globally set and the warning suppressed.
  data.to_excel('1.xls')

由于xlwt软件包不再维护,xlwt引擎将在未来版本的pandas中删除。这是pandas中唯一支持xls格式写入的引擎。安装openpyxl并改为写入xlsx文件。您可以设置选项io.excel.xls文件。写入“xlwt”以消除此警告。虽然此选项已弃用,并且还会引发警告,但可以全局设置它并抑制警告

将data.to_excel('1.xls')改为data.to_excel('1.xlsx')就可以了。

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值