1.to_sql
def to_sql(self, name, con, schema=None, if_exists="fail", index=True, index_label=None, chunksize=None, dtype=None, method=None,):
name: 输出的表名
con: 数据库连接对象
if_exists: 三个模式:fail,若表存在,则不输出;replace;若表存在,覆盖原来表里的数据;append:若表存在,将数据写到原表的后面.默认为fail
index:是否将df的index单独写到一列中
index_label:指定列作为df的index输出,此时index为True
chunksize: 同read_sql
dtype:指定列的输出到数据库中的数据类型.字典形式储存:{column_name: sql_dtype}。常见的数据类型有sqlalchemy.types.INTEGER(), sqlalchemy.types.NVARCHAR(),sqlalchemy.Datetime()等,具体数据类型可以参考这里
DateFrame.to_sql(name='drug_pool', con=self.con, index=False, if_exists='append')
2.to_csv
def to_csv(self, 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, date_format=None, doublequote=True, escapechar=None, decimal="." ):
path_or_buf:字符串或文件句柄,默认为None 文件路径或对象,如果提供“无”,则结果将作为字符串返回。
sep:character,default','输出文件的字段分隔符。
na_rep:string,默认''缺少数据表示
float_format:string,默认无浮点数的格式字符串
columns:序列,可选要写的列
header:布尔值或字符串列表,默认为True写出列名.如果给出了字符串列表,则假定它是列名的别名
index:布尔值,默认为True写行名(索引)
index_label:字符串或序列,或False,默认为None索引列的列标签(如果需要).如果给出None,并且 header和index为True,则使用索引名称.如果DataFrame使用MultiIndex,则应该给出一个序列.如果为False,则不打印索引名称的字段.使用index_label = False可以更轻松地导入R
nanRep:没有不推荐使用,使用na_rep
mode:str Python写模式,默认为'w'
encoding:字符串,可选表示要在输出文件中使用的编码的字符串,默认为Python 2上的“ascii”和Python 3上的“utf-8”。
compression:字符串,可选表示要在输出文件中使用的压缩的字符串,允许的值为'gzip','bz2',仅在第一个参数是文件名时使用
line_terminator:string,默认为'n'要在输出文件中使用的换行符或字符序列
quoting:来自csv模块的可选常量默认为csv.QUOTE_MINIMAL
quotechar:string(长度为1),默认为“”用于引用字段的字符
doublequote:boolean,默认为True控制字段内的quotechar引用
escapechar:string(长度为1),默认为None用于在适当时转义sep和quotechar的字符
chunksize:int或None一次写入的行
tupleize_cols:boolean,默认为False将multi_index列写为元组列表(如果为True)或新建(扩展格式),如果为False)
date_format:string,默认无日期时间对象的格式字符串
decimal:字符串,默认为'.'字符识别为小数点分隔符.例如,对欧洲数据使用'.'版本0.16.0中的新功能。
DateFrame.to_csv(path_or_buf='drug_pool.csv', index=False, encoding='utf-8', mode='a')
3.to_excel
def 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:str或ExcelWriter对象文件路径或现有的ExcelWriter
sheet_name:str,默认为'Sheet1'将包含DataFrame的工作表名称
na_rep:str,默认''缺少数据表示
float_format:str,可选浮点数的格式字符串.例如,float_format="%.2f"将格式为0.1234到0.12
columns:str的序列或列表,可选要写的列
header:bool或str列表,默认为True写出列名.如果给出了字符串列表,则假定它是列名的别名
index:bool,默认为True写行名(索引)
index_label:str或sequence,可选索引列的列标签(如果需要).如果未指定,并且 标头和索引为True,则使用索引名称.如果DataFrame使用MultiIndex,则应该给出一个序列
startrow:int,默认值为0左上角的单元格行转储数据框
startcol:int,默认值为0左上角的单元格列转储数据框
engine:str,可选编写要使用的引擎,'openpyxl'或'xlsxwriter'.您也可以通过选项设置此io.excel.xlsx.writer,io.excel.xls.writer和 io.excel.xlsm.writer
merge_cells:bool,默认为True将MultiIndex和Hierarchical Rows写为合并单元格
encoding:str,可选编码生成的excel文件.只有xlwt是必需的,其他编写者本身支持unicode
inf_rep:str,默认'inf'无限的表示(Excel中没有无限的本机表示)
verbose:bool,默认为True在错误日志中显示更多信息
freeze_panes:int的元组(长度为2),可选指定要冻结的从一开的最底行和最右列.版本0.20.0中的新功能.
DateFrame.to_excel(excel_writer='drug_pool.xlsx', index=False, encoding='utf-8')
4.to_json
def to_json(self, path_or_buf=None, orient=None, date_format=None, double_precision=10, force_ascii=True, date_unit="ms", default_handler=None, lines=False, compression="infer", index=True):
path_or_buf:字符串或文件句柄,可选文件路径或对象.如果未指定,则结果将作为字符串返回.
orient:字符串指示预期的JSON字符串格式.
系列
默认是'索引'
允许的值为:{'split','records','index','table'}
数据帧
默认是'列'
允许的值为:{'split','records','index','columns','values','table'}
JSON字符串的格式
'split':dict like {'index' - > [index],'columns' - > [columns],'data' - > [values]}
'records':列表如[{column - > value},...,{column - > value}]
'index':像{index - > {column - > value}}这样的字典
'columns':像{column - > {index - > value}}这样的字典
'values':只是values数组
'table':类似于''schema':{schema},'data':{data}}描述数据,数据组件就像orient='records'.版本0.20.0已更改.
date_format:{无,'epoch','iso'}日期转换类型.'epoch'= epoch毫秒,'iso'= ISO8601.默认依赖于东方.对于 orient='table',默认为'iso'.对于所有其他方位,默认值为“epoch”.
double_precision:int,默认值为10编码浮点值时使用的小数位数.
force_ascii:bool,默认为True强制编码的字符串为ASCII.
date_unit:string,默认'ms'(毫秒)要编码的时间单位,控制时间戳和ISO8601精度.其中一个's','ms','us','ns'分别为秒,毫秒,微秒和纳秒.
default_handler:callable,默认无如果对象无法以其他方式转换为适合JSON的格式,则调用处理程序.应该接收一个参数,它是要转换的对象并返回一个可序列化的对象.
lines:bool,默认为False如果'orient'是'records',则写出以行分隔的json格式.如果不正确的'orient'将抛出ValueError,因为其他的不是列表.版本0.19.0中的新功能.
compression:{'推断','gzip','bz2','zip','xz',无}表示要在输出文件中使用的压缩的字符串,仅在第一个参数是文件名时使用.默认情况下,压缩是从文件名推断出来的.版本0.21.0中的新功能.在版本0.24.0中更改:添加了“推断”选项并设置为默认值
index:bool,默认为True是否在JSON字符串中包含索引值.index=False仅当orient是'split'或'table'时才支持不包括index().版本0.23.0中的新功能.
DataFrame.to_json(path_or_buf='drug_pool.json')