目录:
1.数据分析模块
2.数据文件导入
3.图形绘制
4.读取数据并可视化分析
1.数据分析模块
import
numpy as nn
#
一维数组numpy.array([元素1,元素2....,元素n])
x = nn.array([
'
2
'
,
'
3
'
,
'
d
'
,
'
g
'
])
#
print(x)
#
创建二维数组格式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)
#
切片:数组[起始下标:最终下表+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()调取头部数据,默认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()调取尾部收据,默认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()统计数据基本情况count元素个数、mean平均数、std标准差、
min列中所有数据中最小值、百分数每一列的分位数、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)
数据转置(行列互换)
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.数据文件导入
import
pandas as pd
导入csv文件
i
= pd.read_csv(
'
文件路径
'
)
#
按照某一列排序
i.sort_values(by=
'
列名
'
)
导入excel文件
j
= pd.read_excel(
'
C:/Users/BLX/Desktop/123.xls
'
)
print
(j)
导入mysql数据库中的数据
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)
导入html数据
pd.read_html(
'
网页源码路径
'
)
导入文本数据
pd.read_table(
'
路径
'
)
3.图形绘制
import
matplotlib.pylab as pyl
import
numpy as npy
散点图
/
折线图plot
x
= [1,2,3,4,8
]
y
= [5,7,2,1,5
]
折线图
pyl.plot(x,y)
#
plot(x轴数据,y轴数据,展现形式(可有可无))
pyl.show()
散点图
pyl.plot(x,y,
'
o
'
)
pyl.show()
颜色
'''
c-cyan-青色
r-red-红色
m-magenta-品红
g-green-绿色
b-blue-蓝色
y-yellow-黄色
k-black-黑色
w-white-白色
'''
pyl.plot(x,y,
'
oc
'
)
线条样式
'''
-直线
--虚线
-.点直线
:细小虚线
'''
pyl.plot(x,y,
'
-.
'
)
点的样式
'''
s方形
h六角形
H六角形
*星形
+加好形
x叉形
d菱形
D菱形
p五角形
'''
pyl.plot(x,y,
'
p
'
)
pyl.show()
标题
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
'
)
#
主标题
pyl.xlabel(
'
ages
'
)
#
x轴标题
pyl.ylabel(
'
temp
'
)
#
y轴标题
pyl.xlim(0,10)
#
x轴范围
pyl.ylim(0.10)
#
y轴范围
pyl.show()
随机数的生成
data
= npy.random.random_integers(1,20,10)
#
(最小值,最大值,个数)生成10个1-20之间的随机数
data2 = npy.random.normal(5.0,2.0,10)
#
(平均数,西格玛,个数)正态分布随机数
正态分布随机数,参考网址: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()
设置直方图的组距、轮廓
data4
= npy.random.random_integers(1,25,100
)
sty
= npy.arange(2,17,2)
#
(起始,结束,每条直方图取值间隔)
pyl.hist(data4,sty,histtype=
'
stepfilled
'
)
#
histtype内参数取消图形轮廓
pyl.show()
子图绘制:各区域内分别写内容
data4
= npy.random.random_integers(1,25,100
)
#
区域1
pyl.subplot(2,2,1)
#
拆分n行,拆分n列,当前绘制区域
x1 = [1,2,3,4,5
]
y1
= [5,2,5,8,9
]
pyl.plot(x1,y1)
区域2
pyl.subplot(
2,2,2
)
x2
= [1,2,3,4,5
]
y2
= [5,2,5,8,9
]
pyl.plot(x2,y2)
区域3
pyl.subplot(
2,1,2)
#
拆分为2行1列的第二个位置
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.读取数据并可视化分析
import
pandas as pd
import
numpy as np
import
matplotlib.pylab as mp
data
= pd.read_csv(
'
E:/python/123.csv
'
)
#
data.shape()#返回(行数,列数)
data2 =
data.T
x1
= data2.values[1]
#
取[第几行][第几列]的数据
y1 = data2.values[2
]
mp.plot(x1,y1)
mp.show()

