您的位置:首页 > 编程语言 > Python开发

Python - MySQL 数据库 与 CSV 读取

2016-04-14 23:01 639 查看
http://www.lfd.uci.edu/~gohlke/pythonlibs/ 下载whl安装包,pip install *.whl 安装
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 14 22:11:22 2016

@author: Administrator
"""

import MySQLdb
import pandas as pd

conn = MySQLdb.connect (host = "127.0.0.1", user = "root", passwd = "film007love", db = "sandbox")
cursor = conn.cursor ()

cursor.execute ("SELECT * FROM mars_tianchi_songs ") # 10842L
rows = cursor.fetchall()

songs = pd.DataFrame( [[ij for ij in i] for i in rows] )
songs.shape # (10842, 6)
songs.rename(columns={0: 'song_id', 1: 'artist_id', 2: 'publish_time', 3: 'song_init_plays', 4:'LANGUAGE', 5:'gender'}, inplace=True);

songs.dtypes

songs.to_hdf("E:\\Tianchi\\songs.h5",'df')
# pd.read_hdf("E:\\Tianchi\\songs.h5",'df')

cursor.close ()
conn.close ()

del songs

songs=pd.read_csv("E:\\Tianchi\\mars_tianchi_songs.csv",header=None)
songs.rename(columns={0: 'song_id', 1: 'artist_id', 2: 'publish_time', 3: 'song_init_plays', 4:'LANGUAGE', 5:'gender'}, inplace=True);
songs.shape #  (10842, 6)

users=pd.read_csv("E:\\Tianchi\\mars_tianchi_user_actions.csv",header=None)
users.rename(columns={0: 'user_id', 1: 'song_id', 2: 'gmt_create', 3: 'action_type', 4:'ds'}, inplace=True);
users.shape #  (5652232, 5)

help(pd.read_csv)
DROP TABLE mars_tianchi_songs ;DROP TABLE mars_tianchi_user_actions ;CREATE TABLE mars_tianchi_user_actions(user_id NVARCHAR(150),song_id NVARCHAR(150),gmt_create NVARCHAR(50),action_type SMALLINT,ds NVARCHAR(10)) ENGINE=INNODB ;CREATE TABLE mars_tianchi_songs( song_id NVARCHAR(150),artist_id NVARCHAR(150),publish_time NVARCHAR(50),song_init_plays NVARCHAR(50),LANGUAGE NVARCHAR(50),gender NVARCHAR(10)) ENGINE=INNODB;TRUNCATE TABLE mars_tianchi_user_actions;TRUNCATE TABLE mars_tianchi_songs;LOAD DATA INFILE 'E:\\Tianchi\\mars_tianchi_songs.csv'   INTO TABLE mars_tianchi_songs    FIELDS TERMINATED BY ','  OPTIONALLY ENCLOSED BY '"' ESCAPED BY '"'   LINES TERMINATED BY '\n';   -- 10842LOAD DATA INFILE 'E:\\Tianchi\\mars_tianchi_user_actions.csv'   INTO TABLE mars_tianchi_user_actions    FIELDS TERMINATED BY ','  OPTIONALLY ENCLOSED BY '"' ESCAPED BY '"'   LINES TERMINATED BY '\n';   -- 5652232SELECT COUNT(0) FROM mars_tianchi_songs ;SELECT COUNT(0) FROM mars_tianchi_user_actions ;SELECT * FROM mars_tianchi_songs LIMIT 10;SELECT * FROM mars_tianchi_user_actions LIMIT 10;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: