加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

Python

(2021-02-11 00:10:31)
Below is my study on Python to mine websites,mssql,excel and stata datasets:

#request website information code
import requests as rq
response=rq.get("http://www.baidu.com")
print(response.status_code)
print(response.text)

#connect mssql
import pymssql
conn=pymssql.connect('(local)','sa','123456','CIS')
cursor=conn.cursor()
sql="create table pytest(id char(10),name char(30),gender char(30))"
cursor.execute(sql)
conn.commit()
sql="insert into pytest(id,name,gender) values (1000,'jack','male')"
cursor.execute(sql)
conn.commit()
sql="insert into pytest(id,name,gender) values (1001,'alice','female')"
cursor.execute(sql)
conn.commit()
sql="select * from pytest"
cursor.execute(sql)
conn.commit()
row=cursor.fetchone()
while row:
    print(row)
    row=cursor.fetchone()
sql="update pytest set id=1003,name='jane' where id=1001"
cursor.execute(sql)
conn.commit()
sql="delete from pytest"
cursor.execute(sql)
conn.commit()
sql="drop table pytest"
cursor.execute(sql)
conn.commit()
cursor.close()
conn.close()

#mine excel data
import xlrd
import matplotlib.pyplot as plt
import numpy as np
xl=xlrd.open_workbook(r'D:\data.xls')
sheet=xl.sheets()[1]
Xs=[]
As=[]
Bs=[]
for i in range(1,10):
    X=sheet.row_values(i)[8]
    Xs.append(X)
    A=sheet.row_values(i)[9]
    As.append(A)
    B=sheet.row_values(i)[4]
    Bs.append(B)
print(Xs,As,Bs)
plt.bar(np.arange(len(Xs)),height=As,width=0.3,color="red")
plt.bar(np.arange(len(Xs))+0.3,height=Bs,width=0.3,color="green")
plt.title('test')
plt.legend('CD')
plt.xlabel('x')
plt.ylabel('y')
plt.show

#connect stata
import ipystata
from ipystata.config import config_stata
config_stata("C:\Program Files (x86)\Stata14\StataMP-64.exe")
import pandas as pd
import ipystata
%%stata
sysuse auto.dta,clear
sum
pwcorr_a *
graph twoway (scatter price mpg)(lfit price mpg)(qfit price mpg)
local x "headroom trunk rep78"
qui:eststo:reg price `x'
qui:eststo:reg mpg `x'
esttab,b(%4.3f) t(%6.2f)
eststo clear
%%stata
*draw world map
use "C:\Users\john\Desktop\workfiles\quantitative analysis method\world-d",clear
rename _ID id
spmap using "C:\Users\john\Desktop\workfiles\quantitative analysis method\world-c",id(id) fcolor(green)
%%stata
*draw China map
use "C:\Users\john\Desktop\workfiles\quantitative analysis method\China_label",clear
spmap using "C:\Users\john\Desktop\workfiles\quantitative analysis method\China_map",id(id) fcolor(red)
%%stata
*draw US map
use "C:\Users\john\Desktop\workfiles\quantitative analysis method\stats",clear
merge 1:1 scode using "C:\Users\john\Desktop\workfiles\quantitative analysis method\trans"
drop _merge
merge 1:1 id using "C:\Users\john\Desktop\workfiles\quantitative analysis method\usdb"
drop if _merge!=3
drop _merge
replace pop1990=pop1990/1e+6
format pop1990 %4.2f
spmap pop1990 using "C:\Users\john\Desktop\workfiles\quantitative analysis method\uscoord" if id!=1 & id!=39 & id!=54 & id!=56,id(id) fcolor(blue)
%%stata
*draw Italy map
use "C:\Users\john\Desktop\workfiles\quantitative analysis method\Italy-RegionsData",clear
spmap using "C:\Users\john\Desktop\workfiles\quantitative analysis method\Italy-RegionsCoordinates",id(id) fcolor(blue)

0

阅读 收藏 喜欢 打印举报/Report
前一篇:长空大云横
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有