数据分析1

import numpy as nn
#创建一维数组
#numpy.array([元素1,元素2,元素3...])
x=numpy.array(['a','9','8','2'])
#创建二维数组
numpy.array([元素1,元素2],[元素3,4】)
x=numpy.array([['a','b','c'],[1,2,3]])

In [9]: x[1]
Out[9]: array(['1', '2', '3'], dtype='<U1')
#排序sort()
x.sort()
#取最大值最小值
y1 =y.max()
#切片
#x[1:3}#1~3

'''pandas'''
import pandas as pda
'''
series #index索引
DataFrams
'''
pda.Series()
a =pda.Series([8,9,2,1])

In [26]: a
Out[26]:
0 8
1 9
2 2
3 1
dtype: int64

eries([8,9,2,1],index=['one','two','three','thour'])

In [30]: b
Out[30]:
one 8
two 9
three 2
thour 1
dtype: int64

c=pda.DataFrame([3,324,6,7],[1,2,3,4])

In [32]: c
Out[32]:
0
1 3
2 324
3 6
4 7

c=pda.DataFrame([[3,324,6,7],[1,2,3,4],[3,4,5,6]],columns=['one','two','three','four'])

In [37]: c
Out[37]:
one two three four
0 3 324 6 7
1 1 2 3 4
2 3 4 5 6
d= pda.DataFrame({'one':1,'two':[1,2,3],'three':55})

In [41]: d
Out[41]:
one three two
0 1 55 1
1 1 55 2
2 1 55 3
d.head()#头部数据,默认前5行
d.head(2)设置为前2行
d.tail()#尾部数据,默认为后5行
d.describe() 统计数量
d.describe()
Out[43]:
one three two
count 3 3 3.0元素个数统计
mean 1 55 2.0平均值
std 0 0 1.0标准差
min 1 55 1.0
25% 1 55 1.5
50% 1 55 2.0中分位数
75% 1 55 2.5
max 1 55 3

d.T 转置矩阵

'''数据导入‘’‘
import pandas as pda
i =pda.read_csv(/home/....)
i.describe()
i.sort_values(by"0")


i =pda.read_excel(/home/....)
#导入mysql数据
import pymysql

In [6]: conn =pymysql.connect('localhost','root','123456',db='hexun')
sql="select *from t1"
i =pda.read_sql(sql,conn)
#导入html数据
pip install html5lib ,brautifulsoup4
pda.read_html("..../.html")
a=pandas.read_html("http://book.douban.com/") 读网页表格!

In [3]: a
Out[3]:
[ 0
0 豆瓣摄影]
pda.read_table()导入文本数据

----------------------------------------------------------
#折线图/散点图
import matplotlib.pylab
import numpy as npy
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.plot(x,y,'c')
'''
c--cyan--青色
r --red--
m--magente--品红
g--green
b--blue--
y --yellow-
k--black--
w--white--
'''
#直方图hist
#线条样式
'''
-直线
--虚线
-.形式

pyl.plot(x,y,'-.')
Out[32]: [<matplotlib.lines.Line2D at 0x7f26e019eac8>]
#点的样式

s---方形
h---六角形
H---六角形
*---星行
+---加号
d--菱形
D--菱形
p--五角形

pyl.plot(x,y)
pyl.title("show") #添加表头,x,y轴名
pyl.xlable("age")
pyl.ylable('temp')

pyl.xlim(0,20)
pyl.ylim(5,18)

#随机数生成
import numpy as npy
npy.random.random_integers(1,20)#
npy.random.random_integers(1,20,10)
Out[45]: array([10, 16, 19, 19, 19, 1, 9, 13, 8, 4])

npy.random.normal(5,2,10)#(均数,西格玛,10个数据)
a=npy.random.normal(10,2,10000)
pyl.hist(a)
pyl.show()

sty=npy.arange(2,14,4)
pyl.hist(data,sty,histtype="stepfilled")

pyl.show()
pyl.subplot(2,2,3)#行,列,当前区域

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

原文地址:https://www.cnblogs.com/sky-ai/p/9951863.html