打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
DataFrame的存储

Pandas中的DataFrame数据既可以存储在SQL数据库中,也可以直接存储在CSV文件中。

数据库

1. dataframe.to_sql()函数将DataFrame数据存储到数据库中。

name :数据表的名称,string, Nameof SQL table

con : 数据库的链接,SQLAlchemyengine or DBAPI2 connection (legacy mode)

if_exists :如果SQL表已经存在的处理方式,{‘fail’, ‘replace’, ‘append’}, default‘fail’

 取消存储, fail: If table exists, donothing.

 替换SQL表,replace: If table exists, drop it,recreate it, and insert data.

 附加在SQL表后,append: If table exists, insertdata. Create if does not exist.

index :是否将行标签存入SQL表,boolean, default TrueWrite DataFrame index as acolumn.

index_label :如果存储行标签,指定该列的名称,string or sequence, defaultNone, Column label for indexcolumn(s). 

2.pandas.read_sql()函数将SQL表读入DataFrame。

sql :要读取的SQL表,string, database table name.

con : 与数据库的链接,SQLAlchemyengine

index_col :指定用于行标签的列,string, optional, column name to use as index for thereturned DataFrame object.

CSV文件

1. dataframe.to_csv()函数将DataFrame数据存储在指定的csv文件中。该函数常用的参数有

columns : 指定需要存储的列,Columnsto write

header :是否把列的名字写入CSV文件, boolean, default True. Write outcolumn names. If a list of string is given it is assumed to bealiases for the column names

index :是否把index写入CSV文件,boolean, default True.Write rowlabels (index)

index_label :如果将index写入CSV文件,那么给出index列的标签(列的名字),string or sequence, or False,default None. 

2.pandas.read_csv()函数将CSV文件中的数据读入DataFrame。

header: CSV文件的列名所在的行, int, list ofints.  Row number(s) to use as the column names,and the start of the data. Defaults to0 if no names passed,otherwise None. 

index_col :指定行标签所在的列,int or sequence or False, defaultNone. Column to use as the row labels of theDataFrame. 

names :列的名称,array-like. List of column names touse. 

总结:

相同点:?

1.存储时,CSV和SQL都默认存储行标签(index),因此需要指定存储行标签列的名字,index_label。

2.读取时,CSV和SQL都需要指定哪一列是行标签(index)列,index_col,不同的是CSV是通过列的序号指定,SQL是通过列的名字指定。?

不同点:

1.SQL一定会存储列标签,CSV可以存储列标签也可以不存储列标签。?

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Pandas常用命令汇总,建议收藏!
13个Pandas奇技淫巧
SparkSession简单介绍
Python数据分析入门教程(一):获取数据源
Pandas 数据结构简介
python数据分析笔记——数据加载与整理
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服