導(dǎo)入數(shù)據(jù)
import pandas as pd
#從Excel文件導(dǎo)入數(shù)據(jù)
df = pd.read_excel('filePath/test.xlsx') #默認(rèn)第一行作為df的列索引
print df
A B C
1 0.743068 0.775753 0.586364
2 0.726336 0.917315 0.770945
3 0.448482 0.062748 0.792973
4 0.481502 0.219382 0.835761
5 0.475752 0.966919 0.491558
6 0.885991 0.252072 0.913809
7 0.076248 0.374731 0.595837
8 0.395501 0.733482 0.228993
9 0.390069 0.493331 0.069293
10 0.679217 0.538165 0.376052
#從CSV文件導(dǎo)入數(shù)據(jù)
#注意encoding,默認(rèn)編碼格式都是utf-8
df = pd.read_csv('/Users/viewstap002/test.csv') #默認(rèn)第一行作為df的列索引
print df
Unnamed: 0 A B C
0 1 0.743068 0.775753 0.586364
1 2 0.726336 0.917315 0.770945
2 3 0.448482 0.062748 0.792973
3 4 0.481502 0.219382 0.835761
4 5 0.475752 0.966919 0.491558
5 6 0.885991 0.252072 0.913809
6 7 0.076248 0.374731 0.595837
7 8 0.395501 0.733482 0.228993
8 9 0.390069 0.493331 0.069293
9 10 0.679217 0.538165 0.376052
#從SQL表/庫(kù)導(dǎo)入數(shù)據(jù) 需要用到 pymysql庫(kù)
import pymysql
sqlConn=pymysql.connect(host=sqlHost,user=sql_user,passwd=sql_passwd,port=sql_port,charset='utf8') #建立數(shù)據(jù)庫(kù)連接
sqlConn.select_db(sqlDB_name) #通過數(shù)據(jù)庫(kù)名稱獲取數(shù)據(jù)庫(kù)
sql ='select * from test_table where test_id = 1' #sql語句
test_table_Frame =pd.read_sql(sql,sqlConn) #獲取到DataFrame
導(dǎo)出數(shù)據(jù)
#導(dǎo)出數(shù)據(jù)到Excel文件
df.to_excel(filename)
# 導(dǎo)出數(shù)據(jù)到CSV文件
df.to_csv(filename)
# 導(dǎo)出數(shù)據(jù)到SQL表
df.to_sql(table_name, connection_object)
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者