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)