pandas.DataFrame写入数据库的实现方式
pandas.DataFrame写入数据库的方式
以mysql数据库为例,需要导入包pymysql。
假设我们已经创建了一个df,现在将其导入数据库中。
写法如下:
import pymysql import pandas as pd from sqlalchemy import create_engine #建立连接,username替换为用户名,passwd替换为密码,test替换为数据库名 conn = create_engine('mysql+pymysql://username:passwd@localhost:3306/test',encoding='utf8') #写入数据,table_name为表名,‘replace'表示如果同名表存在就替换掉 pd.io.sql.to_sql(df, "table_name", conn, if_exists='replace')
Pandas DataFrame数据写入文件和数据库
Pandas是Python下一个开源数据分析的库,它提供的数据结构DataFrame极大的简化了数据分析过程中一些繁琐操作,DataFrame是一张多维的表,大家可以把它想象成一张Excel表单或者Sql表。
之前这篇文章已经介绍了从各种数据源将原始数据载入到dataframe中,这篇文件介绍怎么将处理好的dataframe中的数据写入到文件和数据库中。
创建DataFrame对象
首先我们通过二维ndarray创建一个简单的DataFrame:
import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(3, 4)) df
0 1 2 3 0 1.0492286140081302 -0.7922606407983686 0.020418054868760225 -1.6649819403741724 1 0.3485250628814134 -2.117606544377745 1.466822878437205 -0.9249205656243358 2 1.3073567907490637 -0.7350348086218035 0.2856083175408006 -0.9053483976251634
1. Dataframe写入到csv文件
df.to_csv('D:\\a.csv', sep=',', header=True, index=True)
第一个参数是说把dataframe写入到D盘下的a.csv文件中,参数sep表示字段之间用’,’分隔,header表示是否需要头部,index表示是否需要行号。
2. Dataframe写入到json文件
df.to_json('D:\\a.json')
把dataframe写入到D盘下的a.json文件中,文件的内容为
{"0":{"0":1.049228614,"1":0.3485250629,"2":1.3073567907},"1":{"0":-0.7922606408,"1":-2.1176065444,"2":-0.7350348086},"2":{"0":0.0204180549,"1":1.4668228784,"2":0.2856083175},"3":{"0":-1.6649819404,"1":-0.9249205656,"2":-0.9053483976}}
官方demo:
df = pd.DataFrame([['a', 'b'], ['c', 'd']], index=['row 1', 'row 2'], columns=['col 1', 'col 2']) ########### split ########### df.to_json(orient='split') >'{"columns":["col 1","col 2"], "index":["row 1","row 2"], "data":[["a","b"],["c","d"]]}' ########### index ########### df.to_json(orient='index') >'{"row 1":{"col 1":"a","col 2":"b"},"row 2":{"col 1":"c","col 2":"d"}}' ########### records ########### df.to_json(orient='records') >'[{"col 1":"a","col 2":"b"},{"col 1":"c","col 2":"d"}]' ########### table ########### df.to_json(orient='table') >'{"schema": {"fields": [{"name": "index", "type": "string"}, {"name": "col 1", "type": "string"}, {"name": "col 2", "type": "string"}], "primaryKey": "index", "pandas_version": "0.20.0"}, "data": [{"index": "row 1", "col 1": "a", "col 2": "b"}, {"index": "row 2", "col 1": "c", "col 2": "d"}]}'
3.Dataframe写入到html文件
df.to_html('D:\\a.html')
把dataframe写入到D盘下的a.html文件中,文件的内容为
<table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>0</th> <th>1</th> <th>2</th> <th>3</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>-0.677090</td> <td>0.990133</td> <td>-1.775863</td> <td>0.654884</td> </tr> <tr> <th>1</th> <td>-1.825927</td> <td>-2.262985</td> <td>-0.849212</td> <td>-0.154182</td> </tr> <tr> <th>2</th> <td>0.252012</td> <td>0.464503</td> <td>0.771977</td> <td>0.329159</td> </tr> </tbody> </table>
在浏览器中打开a.html的样式为
4.Dataframe写入到剪贴板中
这个是我认为最为贴心的功能, 一行代码可以将dataframe的内容导入到剪切板中,然后可以复制到任意地方
df.to_clipboard()
5.Dataframe写入到数据库中
df.to_sql('tableName', con=dbcon, flavor='mysql')
第一个参数是要写入表的名字,第二参数是sqlarchmy的数据库链接对象,第三个参数表示数据库的类型,“mysql”表示数据库的类型为mysql。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
pycharm中使用pyplot时报错MatplotlibDeprecationWarning
最近在使用Pycharm中matplotlib作图处理时报错,所以这篇文章主要给大家介绍了关于pycharm中使用pyplot时报错MatplotlibDeprecationWarning的相关资料,需要的朋友可以参考下2023-12-12Pandas DataFrame数据的更改、插入新增的列和行的方法
这篇文章主要介绍了Pandas DataFrame数据的更改、插入新增的列和行的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2019-06-06Python时间差中seconds和total_seconds的区别详解
今天小编就为大家分享一篇Python时间差中seconds和total_seconds的区别详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2019-12-12Python连接SQL server数据库并进行简单查询的操作详解
SQL Server是微软推出的重量级的数据库,本文将给大家详细介绍了一下Python连接SQL server数据库详细流程,并通过代码示例给大家讲解的非常清除,具有一定的参考价值,需要的朋友可以参考下2024-02-02
最新评论