pandas-文件加载

三世轮回 提交于 2019-12-05 11:02:52

读取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')

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!