目錄:
1.數(shù)據(jù)分析模塊
2.數(shù)據(jù)文件導(dǎo)入
3.圖形繪制
4.讀取數(shù)據(jù)并可視化分析
?
1.數(shù)據(jù)分析模塊
import
numpy as nn
#
一維數(shù)組numpy.array([元素1,元素2....,元素n])
x = nn.array([
'
2
'
,
'
3
'
,
'
d
'
,
'
g
'
])
#
print(x)
#
創(chuàng)建二維數(shù)組格式numpy.array([[元素1],[元素2]....,[元素n]])
y = nn.array([[2,3,4],[4,3,4,],[34,4,2
,]])
#
print(y)
#
排序sort()
#
x.sort()
#
print(x)
#
y.sort()
#
print(y)
#
取最大值和最小值
#
y1 = y.min()
#
print(y1)
#
切片:數(shù)組[起始下標(biāo):最終下表+1]
x1 = x[1:3
]
x2
= x[:2
]
x3
= x[1
:]
print
(x1,x2,x3)
import
pandas as pda
#
Series #indes 索引
#
a = pda.Series([8,9,2,1])
b = pda.Series([8,9,2,1],index=[
'
a
'
,
'
b
'
,
'
c
'
,
'
d
'
])
print
(s)
c
= pda.DataFrame([[5,6,4,2],[5,4,2,5],[6,2,5,74
]])
指定列名
d
= pda.DataFrame([[5,6,4,2],[5,4,2,5],[6,2,5,74]],columns=[
'
a
'
,
'
b
'
,
'
c
'
,
'
d
'
])
e
=
pda.DataFrame({
'
a
'
:2
,
'
b
'
:[6,4,7
],
'
c
'
:list(str(919
))
})
head()調(diào)取頭部數(shù)據(jù),默認(rèn)5行
d
= pda.DataFrame([[5,6,4,2],[5,4,2,5],[6,2,5,74]],columns=[
'
a
'
,
'
b
'
,
'
c
'
,
'
d
'
])
e
= d.head(2
)
print
(e)
tail()調(diào)取尾部收據(jù),默認(rèn)5行
d
= pda.DataFrame([[5,6,4,2],[5,4,2,5],[6,2,5,74]],columns=[
'
a
'
,
'
b
'
,
'
c
'
,
'
d
'
])
f
= d.tail(2
)
print
(f)
desctibe()統(tǒng)計(jì)數(shù)據(jù)基本情況count元素個(gè)數(shù)、mean平均數(shù)、std標(biāo)準(zhǔn)差、
min列中所有數(shù)據(jù)中最小值、百分?jǐn)?shù)每一列的分位數(shù)、max列中最大值
d
= pda.DataFrame([[5,6,4,2],[5,4,2,5],[6,2,5,74]],columns=[
'
a
'
,
'
b
'
,
'
c
'
,
'
d
'
])
g
=
d.describe()
print
(g)
數(shù)據(jù)轉(zhuǎn)置(行列互換)
d
= pda.DataFrame([[5,6,4,2],[5,4,2,5],[6,2,5,74]],columns=[
'
a
'
,
'
b
'
,
'
c
'
,
'
d
'
])
d1
=
d.T
print
(d1)
?
2.數(shù)據(jù)文件導(dǎo)入
import
pandas as pd
導(dǎo)入csv文件
i
= pd.read_csv(
'
文件路徑
'
)
#
按照某一列排序
i.sort_values(by=
'
列名
'
)
導(dǎo)入excel文件
j
= pd.read_excel(
'
C:/Users/BLX/Desktop/123.xls
'
)
print
(j)
導(dǎo)入mysql數(shù)據(jù)庫中的數(shù)據(jù)
import
pymysql
conn
= pymysql.connect(host=
'
127.0.0.1
'
,user=
'
root
'
,passwd=
'
root
'
,db=
'
hexun
'
)
sql
=
'
select * from myhexun
'
#
查詢語句
k =
pd.read_sql(sql,conn)
導(dǎo)入html數(shù)據(jù)
pd.read_html(
'
網(wǎng)頁源碼路徑
'
)
導(dǎo)入文本數(shù)據(jù)
pd.read_table(
'
路徑
'
)
?
3.圖形繪制
import
matplotlib.pylab as pyl
import
numpy as npy
散點(diǎn)圖
/
折線圖plot
x
= [1,2,3,4,8
]
y
= [5,7,2,1,5
]
折線圖
pyl.plot(x,y)
#
plot(x軸數(shù)據(jù),y軸數(shù)據(jù),展現(xiàn)形式(可有可無))
pyl.show()
散點(diǎn)圖
pyl.plot(x,y,
'
o
'
)
pyl.show()
顏色
'''
c-cyan-青色
r-red-紅色
m-magenta-品紅
g-green-綠色
b-blue-藍(lán)色
y-yellow-黃色
k-black-黑色
w-white-白色
'''
pyl.plot(x,y,
'
oc
'
)
線條樣式
'''
-直線
--虛線
-.點(diǎn)直線
:細(xì)小虛線
'''
pyl.plot(x,y,
'
-.
'
)
點(diǎn)的樣式
'''
s方形
h六角形
H六角形
*星形
+加好形
x叉形
d菱形
D菱形
p五角形
'''
pyl.plot(x,y,
'
p
'
)
pyl.show()
標(biāo)題
pyl.plot(x,y)
x2
= [1,2,3,5,6,9
]
y2
= [3,1,5,6,4,2
]
pyl.plot(x2,y2)
pyl.title(
'
show
'
)
#
主標(biāo)題
pyl.xlabel(
'
ages
'
)
#
x軸標(biāo)題
pyl.ylabel(
'
temp
'
)
#
y軸標(biāo)題
pyl.xlim(0,10)
#
x軸范圍
pyl.ylim(0.10)
#
y軸范圍
pyl.show()
隨機(jī)數(shù)的生成
data
= npy.random.random_integers(1,20,10)
#
(最小值,最大值,個(gè)數(shù))生成10個(gè)1-20之間的隨機(jī)數(shù)
data2 = npy.random.normal(5.0,2.0,10)
#
(平均數(shù),西格瑪,個(gè)數(shù))正態(tài)分布隨機(jī)數(shù)
正態(tài)分布隨機(jī)數(shù),參考網(wǎng)址:www.mamicode.com/info-detail-507676
.html
直方圖hist
data3
= npy.random.normal(10.0,1.0,1000
)
pyl.hist(data3)
pyl.show()
data4
= npy.random.random_integers(1,25,100
)
pyl.hist(data4)
pyl.show()
設(shè)置直方圖的組距、輪廓
data4
= npy.random.random_integers(1,25,100
)
sty
= npy.arange(2,17,2)
#
(起始,結(jié)束,每條直方圖取值間隔)
pyl.hist(data4,sty,histtype=
'
stepfilled
'
)
#
histtype內(nèi)參數(shù)取消圖形輪廓
pyl.show()
子圖繪制:各區(qū)域內(nèi)分別寫內(nèi)容
data4
= npy.random.random_integers(1,25,100
)
#
區(qū)域1
pyl.subplot(2,2,1)
#
拆分n行,拆分n列,當(dāng)前繪制區(qū)域
x1 = [1,2,3,4,5
]
y1
= [5,2,5,8,9
]
pyl.plot(x1,y1)
區(qū)域2
pyl.subplot(
2,2,2
)
x2
= [1,2,3,4,5
]
y2
= [5,2,5,8,9
]
pyl.plot(x2,y2)
區(qū)域3
pyl.subplot(
2,1,2)
#
拆分為2行1列的第二個(gè)位置
x3 = [1,2,3,4,5,6,7,8
]
y3
= [5,2,5,8,9,3,5,8
]
pyl.plot(x3,y3)
pyl.show()
?
4.讀取數(shù)據(jù)并可視化分析
import
pandas as pd
import
numpy as np
import
matplotlib.pylab as mp
data
= pd.read_csv(
'
E:/python/123.csv
'
)
#
data.shape()#返回(行數(shù),列數(shù))
data2 =
data.T
x1
= data2.values[1]
#
取[第幾行][第幾列]的數(shù)據(jù)
y1 = data2.values[2
]
mp.plot(x1,y1)
mp.show()
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

