美文网首页
python和pandas处理excel(2)

python和pandas处理excel(2)

作者: OURPIECE | 来源:发表于2020-08-13 09:16 被阅读0次

pandas安装

1、安装包

$ pip install pandas


image.png

2、进入python的交互式界面

$ python -i

3、使用Pandas

import pandas as pd
df = pd.DataFrame()
print(df)

4、输出结果

Empty DataFrame
Columns: []
Index: []

5、第一个程序:创建一个空excel文档

import pandas as pd
df = pd.DataFrame() //DataFrame的概念相当于一个sheet
df.to_excel("D:/test.xlsx")

如果出错,提示

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\python37\lib\site-packages\pandas\core\generic.py", line 2029, in to_excel
    engine=engine,
  File "D:\python37\lib\site-packages\pandas\io\formats\excel.py", line 730, in write
    writer = ExcelWriter(stringify_path(writer), engine=engine)
  File "D:\python37\lib\site-packages\pandas\io\excel\_openpyxl.py", line 18, in __init__
    from openpyxl.workbook import Workbook
ModuleNotFoundError: No module named 'openpyxl'

ModuleNotFoundError: No module named 'openpyxl' 表示没有安装openpyxl包
运行下面的命令即可
pip install openpyxl

image.png

如果出错,提示

>>> df = pd.read_excel("E:/test.xlsx")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\python37\lib\site-packages\pandas\util\_decorators.py", line 296, in wrapper
    return func(*args, **kwargs)
  File "D:\python37\lib\site-packages\pandas\io\excel\_base.py", line 304, in read_excel
    io = ExcelFile(io, engine=engine)
  File "D:\python37\lib\site-packages\pandas\io\excel\_base.py", line 867, in __init__
    self._reader = self._engines[engine](self._io)
  File "D:\python37\lib\site-packages\pandas\io\excel\_xlrd.py", line 21, in __init__
    import_optional_dependency("xlrd", extra=err_msg)
  File "D:\python37\lib\site-packages\pandas\compat\_optional.py", line 93, in import_optional_depen
    raise ImportError(msg) from None
ImportError: Missing optional dependency 'xlrd'. Install xlrd >= 1.0.0 for Excel support Use pip or
>>> quit()

ImportError: Missing optional dependency 'xlrd' 表示没有安装xlrd包
运行下面的命令即可
pip install xlrd

image.png

相关文章

网友评论

      本文标题:python和pandas处理excel(2)

      本文链接:https://www.haomeiwen.com/subject/ofrpdktx.html