pandas.DataFrame寫入數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方式
pandas.DataFrame寫入數(shù)據(jù)庫(kù)的方式
以mysql數(shù)據(jù)庫(kù)為例,需要導(dǎo)入包pymysql。
假設(shè)我們已經(jīng)創(chuàng)建了一個(gè)df,現(xiàn)在將其導(dǎo)入數(shù)據(jù)庫(kù)中。
寫法如下:
import pymysql import pandas as pd from sqlalchemy import create_engine #建立連接,username替換為用戶名,passwd替換為密碼,test替換為數(shù)據(jù)庫(kù)名 conn = create_engine('mysql+pymysql://username:passwd@localhost:3306/test',encoding='utf8') ? #寫入數(shù)據(jù),table_name為表名,‘replace'表示如果同名表存在就替換掉 pd.io.sql.to_sql(df, "table_name", conn, if_exists='replace')
Pandas DataFrame數(shù)據(jù)寫入文件和數(shù)據(jù)庫(kù)
Pandas是Python下一個(gè)開源數(shù)據(jù)分析的庫(kù),它提供的數(shù)據(jù)結(jié)構(gòu)DataFrame極大的簡(jiǎn)化了數(shù)據(jù)分析過(guò)程中一些繁瑣操作,DataFrame是一張多維的表,大家可以把它想象成一張Excel表單或者Sql表。
之前這篇文章已經(jīng)介紹了從各種數(shù)據(jù)源將原始數(shù)據(jù)載入到dataframe中,這篇文件介紹怎么將處理好的dataframe中的數(shù)據(jù)寫入到文件和數(shù)據(jù)庫(kù)中。
創(chuàng)建DataFrame對(duì)象
首先我們通過(guò)二維ndarray創(chuàng)建一個(gè)簡(jiǎn)單的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)
第一個(gè)參數(shù)是說(shuō)把dataframe寫入到D盤下的a.csv文件中,參數(shù)sep表示字段之間用’,’分隔,header表示是否需要頭部,index表示是否需要行號(hào)。
2. Dataframe寫入到j(luò)son文件
df.to_json('D:\\a.json')
把dataframe寫入到D盤下的a.json文件中,文件的內(nèi)容為
{"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文件中,文件的內(nèi)容為
<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寫入到剪貼板中
這個(gè)是我認(rèn)為最為貼心的功能, 一行代碼可以將dataframe的內(nèi)容導(dǎo)入到剪切板中,然后可以復(fù)制到任意地方
df.to_clipboard()
5.Dataframe寫入到數(shù)據(jù)庫(kù)中
df.to_sql('tableName', con=dbcon, flavor='mysql')
第一個(gè)參數(shù)是要寫入表的名字,第二參數(shù)是sqlarchmy的數(shù)據(jù)庫(kù)鏈接對(duì)象,第三個(gè)參數(shù)表示數(shù)據(jù)庫(kù)的類型,“mysql”表示數(shù)據(jù)庫(kù)的類型為mysql。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
基于Python實(shí)現(xiàn)植物大戰(zhàn)僵尸游戲的示例代碼
植物大戰(zhàn)僵尸是一款經(jīng)典的塔防類游戲,玩家通過(guò)種植各種植物來(lái)抵御僵尸的攻擊,本文將詳細(xì)介紹如何使用Python和Pygame庫(kù)來(lái)實(shí)現(xiàn)一個(gè)簡(jiǎn)單的植物大戰(zhàn)僵尸游戲,文中通過(guò)代碼示例講解的非常詳細(xì),感興趣的小伙伴跟著小編一起來(lái)看看吧2024-10-10使用Python實(shí)現(xiàn)數(shù)據(jù)庫(kù)的風(fēng)險(xiǎn)識(shí)別
數(shù)據(jù)庫(kù)風(fēng)險(xiǎn)發(fā)現(xiàn)系統(tǒng)旨在識(shí)別和緩解數(shù)據(jù)庫(kù)中的潛在風(fēng)險(xiǎn),如SQL注入,未授權(quán)訪問(wèn)等,下面小編就來(lái)為大家詳細(xì)介紹一下如何使用Python實(shí)現(xiàn)數(shù)據(jù)庫(kù)的風(fēng)險(xiǎn)識(shí)別吧2025-03-03人臉識(shí)別實(shí)戰(zhàn)之Opencv+SVM實(shí)現(xiàn)人臉識(shí)別
這篇文章主要介紹了通過(guò)Opencv+SVM實(shí)現(xiàn)人臉識(shí)別功能,文中的示例代碼介紹詳細(xì),對(duì)于我們學(xué)習(xí)人臉識(shí)別和OpenCV都有一定的幫助,感興趣的小伙伴可以學(xué)習(xí)一下2021-12-12Python從list類型、range()序列簡(jiǎn)單認(rèn)識(shí)類(class)【可迭代】
這篇文章主要介紹了Python從list類型、range()序列簡(jiǎn)單認(rèn)識(shí)類(class),結(jié)合實(shí)例形式分析了list、range及自定義類等可迭代數(shù)據(jù)類型相關(guān)使用技巧,需要的朋友可以參考下2019-05-05pandas pivot_table() 按日期分多列數(shù)據(jù)的方法
今天小編就為大家分享一篇pandas pivot_table() 按日期分多列數(shù)據(jù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-11-11python基礎(chǔ)之reverse和reversed函數(shù)的介紹及使用
我們?cè)谡碣Y料的時(shí)候,有的時(shí)候過(guò)于混亂難以整理,這時(shí)我們不妨可以對(duì)它先進(jìn)行一個(gè)排序,這樣可以方便我們?yōu)g覽,查詢,有利于我們整理資料,下面這篇文章主要給大家介紹了關(guān)于python基礎(chǔ)之reverse和reversed函數(shù)的介紹及使用,需要的朋友可以參考下2022-12-12pytorch 如何使用batch訓(xùn)練lstm網(wǎng)絡(luò)
這篇文章主要介紹了pytorch 如何使用batch訓(xùn)練lstm網(wǎng)絡(luò)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05