Pandas數(shù)據(jù)如何讀取與導出
更新時間:2025年01月22日 11:06:25 作者:王小工
Pandas是一個強大的Python庫,用于數(shù)據(jù)處理和分析,它提供了多種文件格式的數(shù)據(jù)讀取和導出方法,包括CSV、Excel、SQL數(shù)據(jù)庫、JSON等,常用的數(shù)據(jù)讀取方法為`pd.read_csv()`、`pd.read_excel()`等,導出方法為`to_csv()`、`to_excel()`等
Pandas數(shù)據(jù)讀取與導出
Pandas 是一個強大的 Python 庫,用于數(shù)據(jù)處理和分析。它提供了許多函數(shù)來讀取和導入數(shù)據(jù),支持多種文件格式,如 CSV、Excel、SQL 數(shù)據(jù)庫、JSON 等。
以下是一些常用的數(shù)據(jù)讀取和導出方法:
常用方法
格式 | 文件格式 | 讀取函數(shù) | 寫入(導出)函數(shù) |
---|---|---|---|
binary | Excel | read_excel | to_excel |
text | CSV | read_csv read_table | to_csv |
text | JSON | read_json | to_json |
text | 網(wǎng)頁表格HTML | read_html | to_html |
text | 剪切板 | read_clipboard | to_clipboard |
SQ!L | SQL | read_sql | to_sql |
XML | read_xml | read_xml | |
text | Markdown | to_markdown |
其中:
- 讀取函數(shù)一般會賦值給一個變量 df, df = pd.read_()
- 輸出函數(shù)是將變量自身進行操作并輸出 df.to_()
常用函數(shù)方法
Excel 對象
~ pd.ExcelFile 對象 ~ pd.ExcelWriter 對象 ~ pd.ExcelWriter 對象的屬性和方法
讀取數(shù)據(jù)
~ pd.read_csv() ~ pd.read_excel() ~ pd.json_normalize() ~ pd.read_pickle() ~ pd.read_table() ~ pd.DataFrame.from_dict() 從字典創(chuàng)建 DataFrame ~ pd.read_clipboard() 從剪貼板讀取數(shù)據(jù) ~ pd.read_json() 讀取 JSON ~ pd.read_sql() 讀取數(shù)據(jù)庫數(shù)據(jù) ~ pd.read_fwf() 讀取固定寬度格式文件 ~ pd.read_html() 從 HTML 文檔提取表格數(shù)據(jù) ~ pd.read_parquet() 讀取 Parquet 文件
導出數(shù)據(jù)
~ to_csv() 導出為 CSV文件 ~ to_excel() 導出為 Excel 文件 ~ to_dict() 輸出字典 ~ to_pickle() 序列化為 pickle 文件 ~ to_json() 轉換為 JSON 格式字符串 ~ to_html() 轉換為 HTML 表格格式 ~ to_sql() 寫入到關系型數(shù)據(jù)庫 ~ to_parquet() 保存為 parquet 文件
數(shù)據(jù)讀取
- 讀取 CSV 文件
import pandas as pd df = pd.read_csv('file.csv') # 可以使用參數(shù)來調(diào)整讀取行為,如分隔符、缺失值標記、列名等
- 讀取 Excel 文件
df = pd.read_excel('file.xlsx', sheet_name='Sheet1') # 可以指定工作表名稱或索引,或者使用 sheet_name=None 來讀取所有工作表
- 從 SQL 數(shù)據(jù)庫讀取
import sqlite3 # 或其他數(shù)據(jù)庫連接庫 conn = sqlite3.connect('database.db') df = pd.read_sql_query('SELECT * FROM table_name', conn) conn.close() # 對于其他數(shù)據(jù)庫,如 MySQL、PostgreSQL,需要使用相應的連接庫和驅(qū)動
- 讀取 JSON 文件
df = pd.read_json('file.json') # 可以使用 orient 參數(shù)來指定 JSON 數(shù)據(jù)的布局
- 讀取 HTML 表格
df = pd.read_html('http://example.com/page_with_table.html')[0] # read_html 返回一個 DataFrame 列表,通常使用索引 [0] 來獲取第一個表格
- 從剪貼板讀取
df = pd.read_clipboard() # 這對于從 Excel 等應用程序中復制數(shù)據(jù)特別有用
數(shù)據(jù)導出
- 導出到 CSV 文件
df.to_csv('output_file.csv', index=False) # index=False 表示不導出 DataFrame 的索引
- 導出到 Excel 文件
df.to_excel('output_file.xlsx', sheet_name='Sheet1', index=False) # 可以指定工作表名稱和其他選項,如引擎(對于較新的 Pandas 版本,默認引擎為 'openpyxl')
- 導出到 SQL 數(shù)據(jù)庫
conn = sqlite3.connect('database.db') df.to_sql('table_name', conn, if_exists='replace', index=False) conn.close() # if_exists 參數(shù)可以是 'fail'(如果表存在則引發(fā)錯誤)、'replace'(替換表)、'append'(在表中添加數(shù)據(jù))
- 導出到 JSON 文件
df.to_json('output_file.json', orient='records', lines=True) # orient 參數(shù)可以指定 JSON 數(shù)據(jù)的布局,lines=True 表示每行是一個 JSON 對象
- 導出到 HTML 文件
with open('output_file.html', 'w') as f: f.write(df.to_html()) # 也可以使用 pandas 提供的 to_html() 方法生成 HTML 字符串,然后保存到文件中
- 導出到 Excel 的多個工作表
with pd.ExcelWriter('output_file_with_sheets.xlsx') as writer: df1.to_excel(writer, sheet_name='Sheet1', index=False) df2.to_excel(writer, sheet_name='Sheet2', index=False) # 使用 ExcelWriter 上下文管理器可以方便地寫入多個工作表
注意事項:
- 文件路徑:確保文件路徑正確,并且程序有適當?shù)淖x寫權限。
- 數(shù)據(jù)類型:在導出時,注意數(shù)據(jù)類型的兼容性,特別是當數(shù)據(jù)包含特殊字符或日期時間類型時。
- 依賴項:某些導出方法(如到 SQL 數(shù)據(jù)庫)可能需要額外的庫和數(shù)據(jù)庫驅(qū)動。
- 性能:對于大型數(shù)據(jù)集,導出到某些格式(如 Excel)可能會很慢,并且可能會受到內(nèi)存限制。在這種情況下,考慮將數(shù)據(jù)分批導出或使用更適合大數(shù)據(jù)集的格式(如 CSV)。
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
淺談python的elementtree模塊處理中文注意事項
這篇文章主要介紹了淺談python的elementtree模塊處理中文注意事項,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03pyqt 實現(xiàn)在Widgets中顯示圖片和文字的方法
今天小編就為大家分享一篇pyqt 實現(xiàn)在Widgets中顯示圖片和文字的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06python數(shù)字圖像處理之高級形態(tài)學處理
這篇文章主要介紹了python數(shù)字圖像處理之高級形態(tài)學處理,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04