[英]No module named xlsxwriter error while writing pandas df to excel
While writing the pandas code that writes dataframe to Excel.在编写将数据帧写入 Excel 的Pandas代码时。
import pandas as pd
df = pd.DataFrame({'Data': [10, 20, 30, 20, 15, 30, 45]})
writer = pd.ExcelWriter('pandas_simple.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1')
writer.save()
I am getting this error:我收到此错误:
File "/usr/local/lib64/python2.7/site-packages/pandas/io/excel.py", line 1934, in __init__
import xlsxwriter
ImportError: No module named xlsxwriter
Do I need to import xlsxwriter module explicitly in the python file?我需要在 python 文件中显式导入xlsxwriter模块吗?
Install the missing module xlsxwriter
manually by running通过运行手动安装缺少的模块xlsxwriter
pip install xlsxwriter
After the module is installed properly, you do not need to import in manually since it will be imported as an dependency of pandas
.正确安装模块后,您无需手动导入,因为它将作为pandas
的依赖项导入。
Remark: Summarizing the answer from the comments given below the question as discussed here and here备注:总结在此处和此处讨论的问题下方给出的评论中的答案
always first check the # pip list, where you can see the list of packages installed on system.始终首先检查 #pip list,您可以在其中查看系统上安装的软件包列表。
now be sure >>>> pip install XlssWriter (case sensitive)现在确定 >>>> pip install XlssWriter(区分大小写)
next go to your IDE >> just try接下来转到您的 IDE >> 尝试
#import xlsxwriter(no case sensitive). #import xlsxwriter(不区分大小写)。
here is the sample script for your reference:这是供您参考的示例脚本:
import xlsxwriter
# Create a workbook and add a worksheet.
workbook = xlsxwriter.Workbook('Expenses01.xlsx')
worksheet = workbook.add_worksheet()
# Some data we want to write to the worksheet.
expenses = (
['Rent', 1000],
['Gas', 100],
['Food', 300],
['Gym', 50],
)
# Start from the first cell. Rows and columns are zero indexed.
row = 0
col = 0
# Iterate over the data and write it out row by row.
for item, cost in (expenses):
worksheet.write(row, col, item)
worksheet.write(row, col + 1, cost)
row += 1
# Write a total using a formula.
worksheet.write(row, 0, 'Total')
worksheet.write(row, 1, '=SUM(B1:B4)')
workbook.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.