pandas提供了一些用於將表格型數據讀取爲DataFrame對象的函數,期中read_csv和read_table這兩個使用最多
導包:
import pandas as pd
from pandas import Series,DataFrame
import numpy as np
讀取
1. 使用read_csv讀取
pd.read_csv(’./data/SMSSpamCollection’, sep=’\t’, header=None)
pd.read_csv(’./type-.txt’, sep=’-’, header=None)
還可以讀取網上的
pd.read_csv(‘https://raw.githubusercontent.com/datasets/investor-flow-of-funds-us/master/data/weekly.csv’)
2. 使用read_table讀取
pd.read_table(’./data/SMSSpamCollection’, header=None)
table 就是製表符,就是上面的 sep=’\t’
3. 使用read_excel讀取excel
excel 的後綴 “.xlsx”
pd.read_excel(’./test.xlsx’, sheet_name=2, header=[0, 1], index_col=[0, 1])
4. 使用read_sql讀取sqlite文件
先連接數據庫,再讀取
導出
to_csv
weather_2017.to_csv(‘weather_2017.csv’)
to_json
weather_2017.to_json(‘weather_2017.json’)
to_html
weather_2017.to_html(‘weather_2017.html’)
to_sql
先從sqlite種讀取,再寫入到
conn = sqlite3.connect(’./data.sqlite’)
weather_2017.to_sql(‘Weather_2019’, conn)