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。

轉載於:Pandas dataframe數據寫入文件和數據庫

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章