简体   繁体   English

将pandas df写入excel时没有名为xlsxwriter的模块错误

[英]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.

相关问题 xlsxwriter 已安装,但在心理错误中“没有名为 xlsxwriter 的模块” - xlsxwriter installed but “No module named xlsxwriter” in psychopy error 使用xlsxwriter 将pandas df 写入excel 文件? - Write pandas df into excel file with xlsxwriter? 在将 pandas df 写入 excel 时将选定的列类型更改为百分比 - Changing selected columnn types to percentage while writing pandas df to excel 出现错误 - ModuleNotFoundError: No module named 'xlsxwriter' with ExcelWriter - Getting error of - ModuleNotFoundError: No module named 'xlsxwriter' with ExcelWriter ImportError:没有名为“xlsxwriter”的模块 - ImportError: No module named 'xlsxwriter' Pyinstaller “没有名为 xlsxwriter 的模块” - Pyinstaller “No module named xlsxwriter” 将python脚本转换为.exe时出现“ ImportError:没有名为xlsxwriter的模块” - “ImportError: No module named xlsxwriter” while converting python script to .exe (Python 3.6.1)Pandas df到MySQL数据库错误,'ModuleNotFoundError:没有名为'MySQLdb'的模块 - (Python 3.6.1) Pandas df to MySQL db error, 'ModuleNotFoundError: No module named 'MySQLdb' 使用xlsxwriter将数据从Dataframe写入Excel工作表时出错 - Error writing data from Dataframe to excel sheet using xlsxwriter 带有熊猫错误的XlsxWriter - XlsxWriter with Pandas Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM