zoukankan      html  css  js  c++  java
  • 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引擎则是目前更多的功能齐全。


  • 相关阅读:
    Object-C,NSSet,不可变集合
    NYIST 860 又见01背包
    NYIST 1070 诡异的电梯【Ⅰ】
    HDU 1542 Atlantis
    HDU 4756 Install Air Conditioning
    CodeForces 362E Petya and Pipes
    HDU 4751 Divide Groups
    HDU 3081 Marriage Match II
    UVA 11404 Palindromic Subsequence
    UVALIVE 4256 Salesmen
  • 原文地址:https://www.cnblogs.com/hecxx/p/11959855.html
Copyright © 2011-2022 走看看