numpy和Pandas用法讲解

  1 一。 数组要比列表效率高很多
  2     numpy高效的处理数据,提供数组的支持,python默认没有数组。pandas、scipy、matplotlib都依赖numpy。
  3     pandas主要用于数据挖掘,探索,分析
  4     maiplotlib用于作图,可视化
  5     scipy进行数值计算,如:积分,傅里叶变换,微积分
  6     statsmodels用于统计分析
  7     Gensim用于文本挖掘
  8     sklearn机器学习, keras深度学习
  9 二。
 10     numpy和mkl 下载安装
 11     pandas和maiplotlib网络安装
 12     scipy 下载安装
 13     statsmodels和Gensim网络安装
 14 三numpy的操作。
 15     import numpy
 16     # 创建数一维数组组
 17     # numpy.array([元素1,元素2,......元素n])
 18     x = numpy.array(['a', '9', '8', '1'])
 19     # 创建二维数组格式
 20     # numpy.array([[元素1,元素2,......元素n],[元素1,元素2,......元素n],[元素1,元素2,......元素n]])
 21     y = numpy.array([[3,5,7],[9,2,6],[5,3,0]])
 22     # 排序
 23     x.sort()
 24     y.sort()
 25     # 取最大值
 26     y1 = y.max()
 27     # 取最小值
 28     y2 = y.main()
 29     # 切片
 30 四pandas的操作。
 31     import pandas as pda
 32     # 使用pandas生成数据
 33     # Series代表某一串数据 index指定行索引名称,Series索引默认从零开始
 34     # DataFrame代表行列整合出来的数据框,columns 指定列名
 35     a = pda.Series([8, 9, 2, 1], index=['one', 'two', 'three', 'four'])
 36     # 以列表的格式创建数据框
 37     b = pda.DataFrame([[5,6,2,3],[3,5,1,4],[7,9,3,5]], columns=['one', 'two', 'three', 'four'],index=['one', 'two', 'three'])
 38     # 以字典的格式创建数据框
 39     c = pda.DataFrame({
 40         'one':4, # 会自动补全
 41         'two':[6,2,3],
 42         'three':list(str(982))
 43     })
 44     # b.head(行数)# 默认取前5行头
 45     # b.tail(行数)# 默认取后5行尾
 46     # b.describe() 统计数据的情况  count mean std min 25% max
 47     e = b.head()
 48     f = b.describe()
 49     # 数据的转置,及行变成列,列变成行
 50     g = b.T
 51 五python数据的导入
 52     import pandas as pad
 53     f = open('d:/大.csv','rb')
 54     # 导入csv
 55     a = pad.read_csv(f, encoding='python')
 56     # 显示多少行多少列
 57     a.shape()
 58     a.values[0][2] #第一行第三列
 59     # 描述csv数据
 60     b = a.describe()
 61     # 排序
 62     c = a.sort_values()
 63     # 导入excel
 64     d = pad.read_excel('d:/大.xls')
 65     print(d)
 66     print(d.describe())
 67     # 导入mysql
 68     import pymysql
 69     conn = pymysql.connect(host='localhost', user='root', passwd='root', db='')
 70     sql = 'select * from mydb'
 71     e = pad.read_sql(sql, conn)
 72     # 导入html表格数据 需要先安装 html5lib和bs4
 73     g = pad.read_html('https://book.douban.com/subject/30258976/?icn=index-editionrecommend')
 74     # 导入文本数据
 75     h = pad.read_table('d:/lianjie.txt','rb', engine='python')
 76     print(h.describe())
 77 六matplotlib的使用
 78     # 折线图/散点图用plot
 79     # 直方图用hist
 80     import matplotlib.pylab as pyl
 81     import numpy as npy
 82     x = [1,2,4,6,8,9]
 83     y = [5,6,7,8,9,0]
 84     pyl.plot(x, y) #plot(x轴数据,y轴数据,展现形式)
 85     # o散点图,默认是直线 c cyan青色 r red红色 m magente品红色 g green绿色 b blue蓝色 y yellow黄色 w white白色
 86     # -直线  --虚线  -. -.形式  :细小虚线
 87     # s方形 h六角形  *星星  + 加号  x x形式 d菱形 p五角星
 88     pyl.plot(x, y, 'D')
 89     pyl.title('name') #名称
 90     pyl.xlabel('xname') #x轴名称
 91     pyl.ylabel('yname') #y轴名称
 92     pyl.xlim(0,20) #设置x轴的范围
 93     pyl.ylim(2,22) #设置y轴的范围
 94     pyl.show()
 95     # 随机数的生成
 96     data = npy.random.random_integers(1,20,100) #(最小值,最大值,个数)
 97     # 生成具有正态分布的随机数
 98     data2 = npy.random.normal(10.0, 1.0, 10000) #(均值,西格玛,个数)
 99     # 直方图hist
100     pyl.hist(data)
101     pyl.hist(data2)
102     # 设置直方图的上限下限
103     sty = npy.arange(2,20,2) #步长也表示直方图的宽度
104     pyl.hist(data, sty, histtype='stepfilled') # 去除轮廓
105     # 子图的绘制和使用
106     pyl.subplot(2, 2, 2) # (行,列,当前区域)
107     x1 = [2,3,5,8,6,7]
108     y1 = [2,3,5,9,6,7]
109     pyl.plot(x1, y1)
110     pyl.subplot(2, 2, 1) # (行,列,当前区域)
111     x1 = [2,3,5,9,6,7]
112     y1 = [2,3,5,9,6,7]
113     pyl.plot(x1, y1)
114     pyl.subplot(2, 1, 2) # (行,列,当前区域)
115     x1 = [2,3,5,9,6,7]
116     y1 = [2,3,9,5,6,7]
117     pyl.plot(x1, y1)
118     pyl.show()

另外安利一个学习教程:Python3数据科学入门与实战(视频+源码)

分享一个好的IT资源平台:点击进入

猜你喜欢

转载自www.cnblogs.com/alex96/p/12152323.html
今日推荐