• python pandas库的一些使用总结


    pandas用于处理.csv  excel   html  文本等文件。在数据分析方面起到很大的作用。

    • pandas.Series(数据,index=[])         索引数据,默认格式数字按序增加,可以自己设置index,为第一列的索引值。
    • pandas.DataFrame()        创建一个表格样式,横纵坐标索引默认数字递增,可以通过index,columns设置横纵索引。
    • pandas.read_csv()          读取csv文件,如果第一行没有标题,第一行数据会被当做标题使用。
    • pandas.read_excel()     读取excel文件
    • pandas.read_html()      读取html文件,可以安装bs4,html5lib来,能本地文件,能在线,html文件中需要有<table>
    • pandas.read_sql()      读取数据库文件,先连接数据库,定义sql语句,将连接和sql语句作为参数传递。
    • pandas.read_table()      读取txt文本文件。

    写入文件也十分容易,可以将相关数据DataFrame进行创建表格样式,利用to_csv等方法来写入文件。

    如果打开的文件路径中有中文,会出现以下错误:

     File "pandas\_libsparsers.pyx", line 402, in pandas._libs.parsers.TextReader.__cinit__
      File "pandas\_libsparsers.pyx", line 720, in pandas._libs.parsers.TextReader._setup_parser_source
    OSError: Initializing from file failed

    解决方法,在read_csv等读取文件方法中加入engine="python"

    为什么?

    因为在parsers.py中

    # engine is not used in read_fwf() so is factored out of the shared docstring
    _engine_doc = """engine : {'c', 'python'}, optional
        Parser engine to use. The C engine is faster while the python engine is
        currently more feature-complete."""
    引擎可选

    使用解析器引擎。C引擎速度更快,而Python引擎则是目前更多的功能齐全。


  • 相关阅读:
    项目工程化之git提交规范以及 CHANGELOG生成
    移动端 弹窗-内容可滚动,背景不动
    项目readme文件目录生成工具 treer
    css animation动画
    【网络】从套接字到Web服务器
    【网络】图解HTTP-1
    【MySQL】搞懂ACID原则和事务隔离级别
    【MySQL】备份与恢复
    【MySQL】索引
    【Redis】主从复制原理
  • 原文地址:https://www.cnblogs.com/hecxx/p/11959855.html
Copyright © 2020-2023  润新知