pandas-文件加载

读取csv

使用 read_csv 读取

sms = pd.read_csv('./data/SMSSpamCollection', sep='\t',header=None)

sep: 分隔符 header: 不要表头

读取txt

pd.read_csv('./type-.txt', sep='-', header=None)

使用 read_table读取

pd.read_table('./data/SMSSpamCollection', header=None)

 

读取excel表格

pd.read_excel('./read_xlsx.xlsx', sheet_name=2)

 

读取sqlite文件

conn = sqlite3.connect('./data.sqlite')

weather_2017 = pd.read_sql('select * from weather_2017 limit 30', conn, index_col='index')

- 设置行索引index_col

 

写入文件

weather_2017.to_csv('./weather_2017.csv')

weather_2017.to_json('./weather_2017.json')

weather_2017.to_html('./weather_2017.html')

weather_2017.to_sql('weather_2019', conn, if_exists='append')

 

从mysql读取

import pymysql

conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='root', database='maoyan', charset='utf8')

goods_data = pd.read_sql('select * from goods limit 20',conn)

写入mysql

from sqlalchemy import create_engine

engine = create_engine('mysql+mysqldb://root:450502@localhost:3306/?charset=utf8')

goods.to_sql('goods', engine)

 

根据url获取网络上的数据

pd.read_csv('https://raw.githubusercontent.com/datasets/investor-flow-of-funds-us/master/data/weekly.csv')

posted @ 2019-11-24 11:17  Deaseyy  阅读(273)  评论(0编辑  收藏  举报