zoukankan      html  css  js  c++  java
  • Pandas系列(十一)-文件IO操作

    数据分析过程中经常需要进行读写操作,Pandas实现了很多 IO 操作的API,这里简单做了一个列举。

    格式类型数据描述ReaderWriter
    text CSV read_ csv to_csv
    text JSON read_json to_json
    text HTML read_html to_html
    text clipboard read_clipboard to_clipboard
    binary Excel read_excel to_excel
    binary HDF5 read_hdf to_hdf
    binary Feather read_feather to_feather
    binary Msgpack read_msgpack to_msgpack
    binary Stata read_stata to_stata
    binary SAS read_sas
    binary Python Pickle read_pickle to_pickle
    SQL SQL read_sql to_sql
    SQLGoogle Big Query read_gbq to_gbq
     

    主要内容

    • 文件读取
    • 1.read_csv
    • 2.read_excel
    • 3.read_html
    • 4.read_sql
    • 5.read_sql_table
    • 文件保存
    • 1.to_csv
    • 2.to_excel
    • 3.to_sql 

    文件读取

    1 read_csv

    pandas.read_csv(filepath_or_buffer, sep=', ', delimiter=None, header='infer', names=None, index_col=None, usecols=None, squeeze=False, prefix=None, mangle_dupe_cols=True, dtype=None, engine=None, converters=None, true_values=None, false_values=None, skipinitialspace=False, skiprows=None, skipfooter=0, nrows=None, na_values=None, keep_default_na=True, na_filter=True, verbose=False, skip_blank_lines=True, parse_dates=False, infer_datetime_format=False, keep_date_col=False, date_parser=None, dayfirst=False, iterator=False, chunksize=None, compression='infer', thousands=None, decimal=b'.', lineterminator=None, quotechar='"', quoting=0, doublequote=True, escapechar=None, comment=None, encoding=None, dialect=None, tupleize_cols=None, error_bad_lines=True, warn_bad_lines=True, delim_whitespace=False, low_memory=True, memory_map=False, float_precision=None)[source]¶

    参数详解

    • filepath_or_buffer :可以是URL,可用URL类型包括:http, ftp, s3和文件。对于多文件正在准备中本地文件读取。 
    • sep如果不指定参数,则会尝试使用逗号分隔。分隔符长于一个字符并且不是‘s+’,将使用python的语法分析器。并且忽略数据中的逗号。正则表达式例子:’ ’。
    • delimiter定界符,备选分隔符如果指定该参数,则sep参数失效
    • delim_whitespace : 指定空格(例如’ ‘或者’ ‘)是否作为分隔符使用,等效于设定sep=’s+’。如果这个参数设定为True那么delimiter 参数失效。
    • header :指定行数用来作为列名,数据开始行数。如果文件中没有列名,则默认为0【第一行数据】,否则设置为None。如果明确设定 header = 0 就会替换掉原来存在列名。header参数可以是一个list例如:[0,1,3],这个list表示将文件中的这些行作为列标题(意味着每一列有多个标题),介于中间的行将被忽略掉。注意:如果skip_blank_lines=True 那么header参数忽略注释行和空行,所以header=0表示第一行数据而不是文件的第一行。
    import pandas as pd
    
    data = pd.read_csv('friends.csv',encoding='utf-8',header=0)
    data.head()
    Out[109]: 
       Unnamed: 0            ...                              Signature
    0           0            ...              我一定会证明我努力的意义,即使生活一次一次否定它。
    1           1            ...                                    NaN
    2           2            ...                                    NaN
    3           3            ...                                    NaN
    4           4            ...                        希望外表的放肆遮得住内心的柔弱
    [5 rows x 6 columns]
    from io import StringIO
    import pandas as pd
    data = pd.read_csv('friends.csv',encoding='utf-8',header=None)
    data.head()
    Out[110]: 
         0         1            ...                 4                          5
    0  NaN  NickName            ...              City                  Signature
    1  0.0       张亚飞            ...               NaN  我一定会证明我努力的意义,即使生活一次一次否定它。
    2  1.0     Messi            ...                晋城                        NaN
    3  2.0         夕            ...                晋城                        NaN
    4  3.0    Irving            ...               NaN                        NaN
    [5 rows x 6 columns]
    • names :用于结果的列名列表,如果数据文件中没有列标题行,就需要执行 header=None。names属性在header之前运行默认列表中不能出现重复,除非设定参数mangle_dupe_cols=True。
    In [32]: a = pd.read_csv('out.log',names='ko')
    
    In [33]: a
    Out[33]:
               k     o
    0       book  kook
    1      joke2  dddd
    2      fang3   NaN
    3      test1   NaN
    4      test2   NaN
    5      test3   NaN
    6  1997/10/2   NaN
    • index_col :用作行索引的列编号或者列名,如果给定一个序列则有多个行索引。
    • usecols:返回一个数据子集,该列表中的值必须可以对应到文件中的位置(数字可以对应到指定的列)或者是字符传为文件中的列名。例如:usecols有效参数可能是 [0,1,2]或者是 [‘foo’, ‘bar’, ‘baz’]。使用这个参数可以加快加载速度并降低内存消耗。
    • prefix:在没有列标题时,也就是header设定为None,给列添加前缀。例如:添加prefix= ‘X’ 使得列名称成为 X0, X1, …
    • dtype: 每列数据的数据类型。例如 {‘a’: np.float64, ‘b’: np.int32}
    • skipinitialspace:忽略分隔符后的空白(默认为False,即不忽略).
    • skiprows :需要忽略的行数(从文件开始处算起),或需要跳过的行号列表(从0开始)。
    • nrows :需要读取的行数(从文件头开始算起)。
    • na_values :一组用于替换NA/NaN的值。如果传参,需要制定特定列的空值。默认为‘1.#IND’, ‘1.#QNAN’, ‘N/A’, ‘NA’, ‘NULL’, ‘NaN’, ‘nan’`.
    • keep_default_na:如果指定na_values参数,并且keep_default_na=False,那么默认的NaN将被覆盖,否则添加
    • na_filter:是否检查丢失值(空字符串或者是空值)。对于大文件来说数据集中没有空值,设定na_filter=False可以提升读取速度。
    • skip_blank_lines :如果为True,则跳过空行;否则记为NaN。

    2 read_excel

    pd.read_excel(io, sheetname=0,header=0,skiprows=None,index_col=None,names=None,
                    arse_cols=None,date_parser=None,na_values=None,thousands=None, 
                    convert_float=True,has_index_names=None,converters=None,dtype=None,
                    true_values=None,false_values=None,engine=None,squeeze=False,**kwds)
    

      重要参数详解

    • io :excel 路径
    • sheetname:默认是sheetname为0,返回多表使用sheetname=[0,1],若sheetname=None是返回全表 。注意:int/string返回的是dataframe,而none和list返回的是dict of dataframe。
    • header :指定作为列名的行,默认0,即取第一行,数据为列名行以下的数据;若数据不含列名,则设定 header = None;
    • skiprows:省略指定行数的数据
    • skip_footer:省略从尾部数的行数据
    • index_col :指定列为索引列,也可以使用 u’string’
    • names:指定列的名字,传入一个list数据

    3 read_html

    pandas.read_html(io, match='.+', flavor=None, header=None, index_col=None, skiprows=None, attrs=None, parse_dates=False, tupleize_cols=None, thousands=', ', encoding=None, decimal='.', converters=None, na_values=None, keep_default_na=True)[source]
    

     参数详解

    • io : str or file-like

    A URL, a file-like object, or a raw string containing HTML. Note that lxml only accepts the http, ftp and file url protocols. If you have a URL that starts with 'https'you might try removing the 's'.

    接收网址、文件、字符串。网址不接受https,尝试去掉s后爬去

    • match : str or compiled regular expression, optional

    The set of tables containing text matching this regex or string will be returned. Unless the HTML is extremely simple you will probably need to pass a non-empty string here. Defaults to ‘.+’ (match any non-empty string). The default value will return all tables contained on a page. This value is converted to a regular expression so that there is consistent behavior between Beautiful Soup and lxml.

    正则表达式,返回与正则表达式匹配的表格。

    • flavor : str or None, container of strings

    The parsing engine to use. ‘bs4’ and ‘html5lib’ are synonymous with each other, they are both there for backwards compatibility. The default of None tries to use lxml to parse and if that fails it falls back on bs4 + html5lib.

    解析器默认为‘lxml’

    • header : int or list-like or None, optional

    The row (or list of rows for a MultiIndex) to use to make the columns headers.

    指定列标题所在的行,list为多重索引

    • index_col : int or list-like or None, optional

    The column (or list of columns) to use to create the index.

    指定行标题对应的列,list为多重索引

    • skiprows : int or list-like or slice or None, optional

    0-based. Number of rows to skip after parsing the column integer. If a sequence of integers or a slice is given, will skip the rows indexed by that sequence. Note that a single element sequence means ‘skip the nth row’ whereas an integer means ‘skip n rows’.

    跳过第n行(序列标示)或跳过n行(整数标示)

    • attrs : dict or None, optional

    This is a dictionary of attributes that you can pass to use to identify the table in the HTML. These are not checked for validity before being passed to lxml or Beautiful Soup. However, these attributes must be valid HTML table attributes to work correctly. For example,

    attrs = {'id': 'table'}
    

    is a valid attribute dictionary because the ‘id’ HTML tag attribute is a valid HTML attribute for any HTML tag as per this document.

    attrs = {'asdf': 'table'}
    

    is not a valid attribute dictionary because ‘asdf’ is not a valid HTML attribute even if it is a valid XML attribute. Valid HTML 4.01 table attributes can be found here. A working draft of the HTML 5 spec can be found here. It contains the latest information on table attributes for the modern web.

    传递一个字典,标示表格的属性值。

    • parse_dates : bool, optional

    boolean or list of ints or names or list of lists or dict, default False

    • boolean. If True -> try parsing the index.
    • list of ints or names. e.g. If [1, 2, 3] -> try parsing columns 1, 2, 3 each as a separate date column.
    • list of lists. e.g. If [[1, 3]] -> combine columns 1 and 3 and parse as a single date column.
    • dict, e.g. {‘foo’ : [1, 3]} -> parse columns 1, 3 as date and call result ‘foo’

    If a column or index contains an unparseable date, the entire column or index will be returned unaltered as an object data type. For non-standard datetime parsing, use pd.to_datetime after pd.read_csv

    Note: A fast-path exists for iso8601-formatted dates.

    解析日期

    • tupleize_cols : bool, optional

    If False try to parse multiple header rows into a MultiIndex, otherwise return raw tuples. Defaults to False.

    Deprecated since version 0.21.0: This argument will be removed and will always convert to MultiIndex

    不推荐使用

    • thousands : str, optional

    Separator to use to parse thousands. Defaults to ','.

    千位分隔符

    • encoding : str or None, optional

    The encoding used to decode the web page. Defaults to None.``None`` preserves the previous encoding behavior, which depends on the underlying parser library (e.g., the parser library will try to use the encoding provided by the document).

    解码方式,默认使用文档提供的编码

    • decimal : str, default ‘.’

    Character to recognize as decimal point (e.g. use ‘,’ for European data).

    New in version 0.19.0.

    小数点标示,默认使用“.”

    • converters : dict, default None

    Dict of functions for converting values in certain columns. Keys can either be integers or column labels, values are functions that take one input argument, the cell (not column) content, and return the transformed content.

    New in version 0.19.0.

    转换某些列的函数的字典:键为列名或者整数,值为转换函数,函数只能传入一个参数,就是该列单元格的值。

    • na_values : iterable, default None

    Custom NA values

    New in version 0.19.0.

    标示那些为NA值

    • keep_default_na : bool, default True

    If na_values are specified and keep_default_na is False the default NaN values are overridden, otherwise they’re appended to

    New in version 0.19.0.

    保持默认的NA值,与na_values一起使用

    # -*- coding: utf-8 -*-
    
    """
    @Datetime: 2018/11/11
    @Author: Zhang Yafei
    """
    from multiprocessing import Pool
    
    import pandas
    import requests
    import os
    
    BASE_DIR = os.path.dirname(os.path.abspath(__file__))
    HTML_DIR = os.path.join(BASE_DIR,'药品商品名通用名称数据库')
    
    if not os.path.exists(HTML_DIR):
        os.mkdir(HTML_DIR)
    
    name_list = [] 
    if os.path.exists('drug_name.csv'):
        data = pandas.read_csv('drug_name.csv',encoding='utf-8')
        
    header = {
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
            'Accept-Encoding': 'gzip, deflate',
            'Accept-Language': 'zh-CN,zh;q=0.9',
            'Cache-Control': 'max-age=0',
            'Connection': 'keep-alive',
            'Content-Length': '248',
            'Content-Type': 'application/x-www-form-urlencoded',
            'Cookie': 'JSESSIONID=0000ixyj6Mwe6Be4heuHcvtSW4C:-1; Hm_lvt_3849dadba32c9735c8c87ef59de6783c=1541937281; Hm_lpvt_3849dadba32c9735c8c87ef59de6783c=1541940406',
            'Upgrade-Insecure-Requests': '1',
            'Origin': 'http://pharm.ncmi.cn',
            'Referer': 'http://pharm.ncmi.cn/dataContent/dataSearch.do?did=27',
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',
        }
    
    
    def spider(page):
        adverse_url = 'http://pharm.ncmi.cn/dataContent/dataSearch.do?did=27'
        form_data = {
            'method': 'list',
            'did': 27,
            'ec_i': 'ec',
            'ec_crd': 15,
            'ec_p': page,
            'ec_rd': 15,
            'ec_pd': page,
        }
        response = requests.post(url=adverse_url,headers=header,data=form_data)
        filename = '{}.html'.format(page)
        with open(filename,'w',encoding='utf-8') as f:
            f.write(response.text)
        print(filename,'下载完成')
    
    
    def get_response(page):
        file = os.path.join(HTML_DIR,'{}.html')
        with open(file.format(page),'r',encoding='utf-8') as f:
            response = f.read()
        return response
    
    
    def parse(page):
        response = get_response(page)
        result = pandas.read_html(response,attrs={'id':'ec_table'})[0]
        data = result.iloc[:,:5]
        data.columns = ['序号','批准文号','药品中文名称','药品商品名称','生产单位']
        if page==1:
            data.to_csv('drug_name.csv',mode='w',encoding='utf_8_sig',index=False)
        else:
            data.to_csv('drug_name.csv',mode='a',encoding='utf_8_sig',header=False,index=False)
        print('第{}页数据存取完毕'.format(page))
    
    def get_unparse_data():
        if os.path.exists('drug_name.csv'):
            pages = data['序号']
            pages = list(set(range(1,492))-set(pages.values))
        else:
            pages = list(range(1,492))
        return pages
        
    def download():
         pool = Pool()
         pool.map(spider,list(range(1,492)))
         pool.close()
         pool.join()
        
        
    def write_to_csv():
        pages = get_unparse_data()
        print(pages)
        list(map(parse,pages))    
        
    def new_data(chinese_name):
        trade_name = '/'.join(set(data[data.药品中文名称==chinese_name].药品商品名称))
        name_list.append(trade_name)
          
    def read_from_csv():
        name = data['药品中文名称'].values
        print(len(name))
        chinese_name = list(set(data['药品中文名称'].values))
        list(map(new_data,chinese_name))
        df_data = {'药品中文名称':chinese_name,'药品商品名称':name_list}
        new_dataframe = pandas.DataFrame(df_data)
        new_dataframe.to_csv('unique_chinese_name.csv',mode='w',encoding='utf_8_sig',index=False)
        return new_dataframe    
        
    def main():
    #    download()    
    #    write_to_csv()
        return read_from_csv()
    
    if __name__ == '__main__':
        drugname_dataframe = main()
    药品商品名和通用名信息下载

    4 read_sql

    pandas.read_sql(sql, con, index_col=None, coerce_float=True, params=None, parse_dates=None, columns=None, chunksize=None)

    效果:将SQL查询或数据库表读入DataFrame。

    此功能是一个方便的包装和 (为了向后兼容)。它将根据提供的输入委派给特定的功能。SQL查询将被路由到,而数据库表名将被路由到。请注意,委派的功能可能有更多关于其功能的特定说明,此处未列出。

    参数详解

    • sql : string or SQLAlchemy Selectable (select or text object)

    SQL query to be executed or a table name.

    要执行的SQL查询或表名。

    • con : SQLAlchemy connectable (engine/connection) or database string URI

    or DBAPI2 connection (fallback mode)

    Using SQLAlchemy makes it possible to use any DB supported by that library. If a DBAPI2 object, only sqlite3 is supported.

    或DBAPI2连接(后备模式)

    使用SQLAlchemy可以使用该库支持的任何数据库。如果是DBAPI2对象,则仅支持sqlite3。

    • index_col : string or list of strings, optional, default: None

    Column(s) to set as index(MultiIndex).

    要设置为索引的列(MultiIndex)。

    • coerce_float : boolean, default True

    Attempts to convert values of non-string, non-numeric objects (like decimal.Decimal) to floating point, useful for SQL result sets.

    尝试将非字符串,非数字对象(如decimal.Decimal)的值转换为浮点,这对SQL结果集很有用。

    • params : list, tuple or dict, optional, default: None

    List of parameters to pass to execute method. The syntax used to pass parameters is database driver dependent. Check your database driver documentation for which of the five syntax styles, described in PEP 249’s paramstyle, is supported. Eg. for psycopg2, uses %(name)s so use params={‘name’ : ‘value’}

    • parse_dates : list or dict, default: None
    • List of column names to parse as dates.

            要解析为日期的列名列表。

    • Dict of {column_name: format string} where format string is strftime compatible in case of parsing string times, or is one of (D, s, ns, ms, us) in case of parsing integer timestamps.

            在解析字符串时,格式字符串是strftime兼容的格式字符串,或者是(D、s、ns、ms、us),以防解析整型时间戳。

    • Dict of {column_name: arg dict}, where the arg dict corresponds to the keyword arguments of pandas.to_datetime() Especially useful with databases without native Datetime support, such as SQLite.

            {column_name:arg dict}的字典,其中arg dict对应于pandas.to_datetime()的关键字参数。对于没有本机Datetime支持的数据库(如SQLite)特别有用。

    • columns : list, default: None

    List of column names to select from SQL table (only used when reading a table).

    从SQL表中选择的列名列表(仅在读取表时使用)。

    • chunksize : int, default None

    If specified, return an iterator where chunksize is the number of rows to include in each chunk.

    如果指定,则返回一个迭代器,其中chunksize是要包含在每个块中的行数。

    使用案例

    import pymysql
    import pandas as pd
     
    con = pymysql.connect(host="127.0.0.1",user="root",password="password",db="world")
    # 读取sql
    data_sql=pd.read_sql("SQL查询语句",con)
    # 存储
    data_sql.to_csv("test.csv")

    5 read_sql_table

    pandas.read_sql_table(table_name, con, schema=None, index_col=None, coerce_float=True, parse_dates=None, columns=None, chunksize=None)[source]
    

    效果:将SQL数据库表读入DataFrame。

    给定一个表名和一个SQLAlchemy可连接,返回一个DataFrame。此功能不支持DBAPI连接。

    参数详解

    • table_name : string

    Name of SQL table in database.

    数据库中SQL表的名称。

    • con : SQLAlchemy connectable (or database string URI)

    SQLite DBAPI connection mode not supported.

    不支持SQLite DBAPI连接模式。

    • schema : string, default None

    Name of SQL schema in database to query (if database flavor supports this). Uses default schema if None (default).

    要查询的数据库中的SQL模式的名称(如果数据库flavor支持此功能)。如果为None(默认值),则使用默认架构。

    • index_col : string or list of strings, optional, default: None

    Column(s) to set as index(MultiIndex).

    要设置为索引的列(MultiIndex)。

    • coerce_float : boolean, default True

    Attempts to convert values of non-string, non-numeric objects (like decimal.Decimal) to floating point. Can result in loss of Precision.

    尝试将非字符串,非数字对象(如decimal.Decimal)的值转换为浮点值。可能导致精度损失。

    • parse_dates : list or dict, default: None
    • List of column names to parse as dates.

            要解析为日期的列名列表。

    • Dict of {column_name: format string} where format string is strftime compatible in case of parsing string times or is one of (D, s, ns, ms, us) in case of parsing integer timestamps.

            {column_name:format string}的字典,其中格式字符串在解析字符串时间时与strftime兼容,或者在解析整        数时间戳的情况下是(D,s,ns,ms,us)之一。

    • Dict of {column_name: arg dict}, where the arg dict corresponds to the keyword arguments of pandas.to_datetime() Especially useful with databases without native Datetime support, such as SQLite.

            {column_name:arg dict}的字典,其中arg dict对应于pandas.to_datetime()的关键字参数。对于没有本机Datetime支持的数据库(如SQLite)特别有用。

    • columns : list, default: None

    List of column names to select from SQL table

    从SQL表中选择的列名列表

    • chunksize : int, default None

    If specified, returns an iterator where chunksize is the number of rows to include in each chunk.

    如果指定,则返回一个迭代器,其中chunksize是要包含在每个块中的行数。

    使用案例

    import pandas as pd
    import pymysql
    from sqlalchemy import create_engine
     
    con = create_engine('mysql+pymysql://user_name:password@127.0.0.1:3306/database_name')
    data = pd.read_sql_table("table_name", con)
    data.to_csv("table_name.csv")

    文件保存

     1 to_csv

    DataFrame.to_csv(path_or_buf=None, sep=', ', na_rep='', float_format=None, columns=None, header=True, index=True, index_label=None, mode='w', encoding=None, compression='infer', quoting=None, quotechar='"', line_terminator=None, chunksize=None, tupleize_cols=None, date_format=None, doublequote=True, escapechar=None, decimal='.')[source]¶
    

      参数详解

    • path_or_buf=None: string or file handle, default None

    File path or object, if None is provided the result is returned as a string.

    字符串或文件句柄,默认无文件

    路径或对象,如果没有提供,结果将返回为字符串。

    • sep : character, default ‘,’

    Field delimiter for the output file.

    默认字符 ‘ ,’

    输出文件的字段分隔符。

    • na_rep : string, default ‘’

    Missing data representation

    字符串,默认为 ‘’

    浮点数格式字符串

    • float_format : string, default None

    Format string for floating point numbers

    字符串,默认为 None

    浮点数格式字符串

    • columns : sequence, optional Columns to write

    顺序,可选列写入

    • header : boolean or list of string, default True

    Write out the column names. If a list of strings is given it is assumed to be aliases for the column names

    字符串或布尔列表,默认为true

    写出列名。如果给定字符串列表,则假定为列名的别名。

    • index : boolean, default True

    Write row names (index)

    布尔值,默认为Ture

    写入行名称(索引)

    • index_label : string or sequence, or False, default None。Column label for index column(s) if desired. If None is given, and header and index are True, then the index names are used. A sequence should be given if the DataFrame uses MultiIndex. If False do not print fields for index names. Use index_label=False for easier importing in R

    字符串或序列,或False,默认为None

    如果需要,可以使用索引列的列标签。如果没有给出,且标题和索引为True,则使用索引名称。如果数据文件使用多索引,则应该使用这个序列。如果值为False,不打印索引字段。在R中使用index_label=False 更容易导入索引.

    • mode : str

    模式:值为‘str’,字符串

    Python写模式,默认“w”

    • encoding : string, optional

    编码:字符串,可选

    表示在输出文件中使用的编码的字符串,Python 2上默认为“ASCII”和Python 3上默认为“UTF-8”。

    • compression : string, optional

    字符串,可选项

    表示在输出文件中使用的压缩的字符串,允许值为“gzip”、“bz2”、“xz”,仅在第一个参数是文件名时使用。

    • line_terminator : string, default ‘ ’

    字符串,默认为 ‘ ’

    在输出文件中使用的换行字符或字符序列

    • quoting : optional constant from csv module

    CSV模块的可选常量

    默认值为to_csv.QUOTE_MINIMAL。如果设置了浮点格式,那么浮点将转换为字符串,因此csv.QUOTE_NONNUMERIC会将它们视为非数值的。

    • quotechar : string (length 1), default ‘”’

    字符串(长度1),默认“”

    用于引用字段的字符

    • doublequote : boolean, default True

    布尔,默认为Ture

    控制一个字段内的quotechar

    • escapechar : string (length 1), default None

    字符串(长度为1),默认为None

    在适当的时候用来转义sep和quotechar的字符

    • chunksize : int or None

    一次写入行

    • tupleize_cols : boolean, default False

    布尔值 ,默认为False

    从版本0.21.0中删除:此参数将被删除,并且总是将多索引的每行写入CSV文件中的单独行

    (如果值为false)将多索引列作为元组列表(如果TRUE)或以新的、扩展的格式写入,其中每个多索引列是CSV中的一行。

    • date_format : string, default None。字符串,默认为None.字符串对象转换为日期时间对象
    • decimal: string, default ‘.’字符串,默认’。’字符识别为小数点分隔符。例如。欧洲数据使用 ​​’,’

    注意事项:

    • 1、一般情况下我们用utf-8编码进行保存,如果出现中文编码错误,则可以依次换用gbk,gb2312 , gb18030,一般总能成功的,本例中用utf-8
    • 2、to_csv方法,具体参数还有很多,可以去看官方文档,这里提到一个index = False参数,表示保存csv的时候,我们不保存pandas 的Data frame的行索引1234这样的序号,默认情况不加的话是index = True,会有行号(如下图),这点在保存数据库mysql的时候体现尤其明显,不注意的话可能会出错

    2 to_excel

    to_excel(self, excel_writer, sheet_name='Sheet1', na_rep='', float_format=None,columns=None, 
    header=True, index=True, index_label=None,startrow=0, startcol=0, engine=None, 
    merge_cells=True, encoding=None,inf_rep='inf', verbose=True, freeze_panes=None)

    常用参数解析

    • excel_writer : ExcelWriter目标路径
    • sheet_name :excel表名命名
    • na_rep : 缺失值填充 ,可以设置为字符串
    • columns :选择输出的的列存入
    • header :指定作为列名的行,默认0,即取第一行,数据为列名行以下的数据;若数据不含列名,则设定 header = None;
    • index:默认为True,显示index,当index=False 则不显示行索引(名字)
    • index_label:设置索引列的列名
    writer = pd.ExcelWriter('data/excel.xlsx')
    df.to_excel(writer, sheet_name='user', index=False)
    writer.save()  

    多张表保存到一个excel文件

    with pd.ExcelWriter(path="结果.xlsx") as writer:
        exam_data.to_excel(excel_writer=writer, sheet_name='试题数据', index=False)
        student_total_score.to_excel(excel_writer=writer, sheet_name='学生总成绩', index=False)
        student_semester_total.to_excel(excel_writer=writer, sheet_name='每个学生各学期总成绩', index=False)
        course_avg_score.to_excel(excel_writer=writer, sheet_name='各门课程平均成绩', index=False)
        greater_than_avg_student.to_excel(excel_writer=writer, sheet_name='各学期大于本课程平均成绩的学生姓名及成绩', index=False)
        writer.save()

    补充:固定输出列的顺序

    data = pd.DataFrame(data=data_list)
    # 固定列表的输出顺序
    data = data.loc[:, columns]

    3 to_sql

    import pandas as pd
    data = [
           {"name":"张三","age":18,"city":"北京"},
           {"name":"李四","age":19,"city":"上海"},
           {"name":"王五","age":20,"city":"广州"},
           {"name":"赵六","age":21,"city":"深圳"},
           {"name":"孙七","age":22,"city":"武汉"}
    ]
    df = pd.DataFrame(data,columns=["name","age","city"])
    df
     nameagecity
    0 张三 18 北京
    1 李四 19 上海
    2 王五 20 广州
    3 赵六 21 深圳
    4 孙七 22 武汉
     
     
    from sqlalchemy import create_engine
    
    table_name = "user"
    
    engine = create_engine(
        "mysql+pymysql://root:0000@127.0.0.1:3306/db_test?charset=utf8",
        max_overflow=0,  # 超过连接池大小外最多创建的连接
        pool_size=5,  # 连接池大小
        pool_timeout=30,  # 池中没有线程最多等待的时间,否则报错
        pool_recycle=-1  # 多久之后对线程池中的线程进行一次连接的回收(重置)
    )
    conn = engine.connect()
    df.to_sql(table_name, conn, if_exists='append',index=False)

    • 上面代码已经实现将我们构造的df数据保存MySQL,现在提一些注意点

    注意事项:
    1、我们用的库是sqlalchemy,官方文档提到to_sql是被sqlalchemy支持
    文档地址:
    http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_sql.html
    2、数据库配置用你自己的数据库配置,db_flag为数据库类型,根据不同情况更改,在保存数据之前,要先创建数据库字段。

    3. engine_config为数据库连接配置信息
    4、create_engine是根据数据库配置信息创建连接对象
    5、if_exists = 'append',追加数据
    6、index = False 保存时候,不保存df的行索引,这样刚好df的3个列和数据库的3个字段一一对应,正常保存,如果不设置为false的话,数据相当于4列,跟MySQL 3列对不上号,会报错

    index=False)
  • 相关阅读:
    软件项目“免坑”指南
    软件项目质量保证——编码规范
    从Web借鉴UI设计
    C#学习笔记——面向对象、面向组件以及类型基础
    关系数据库设计
    Winform开发框架之插件化应用框架实现
    桌面程序界面设计分享
    2-Bom
    1-简介
    测试
  • 原文地址:https://www.cnblogs.com/zhangyafei/p/10257379.html
Copyright © 2011-2022 走看看