美文网首页
「数据分析」02数据源的导入与matplotlib模块的使用

「数据分析」02数据源的导入与matplotlib模块的使用

作者: 林拂晓 | 来源:发表于2020-01-22 19:59 被阅读0次

1.数据导入

在python数据分析中,可以使用pandas模块导入数据。

使用前提:

import pandas

(1)csv数据导入

csvdata=pandas.read_csv("G:/DA/product.csv",engine="python")

ps:csv数据排序by="列名"

csvdata.sort_values(by="price")

【注】①engine的参数有三个:'c','python','python_fwf';

②导入报错解决方法:

>加上参数engine='python'

>加上参数encoding='utf-8'encoding='gb2312'

>先打开再读取:

df=open("G:/DA/product.csv","r")

data=pda.read_csv(df)

(2)excel数据导入

exldata=pandas.read_excel("G:/DA/product.xls")

(3)mysql数据读取

import pymysql

conn=pymysql.connect(host="localhost",user="root",passwd="123456",db="jingdong")

sql="select * from product"

dbdata=pandas.read_sql(sql,conn)

(4)导入html数据(直接从html网页中加载对应的table表格的数据,需要安装html5lib模块和beautifulsoup4模块)

htmldata=pandas.read_html("G:/DA/product.html")

(5)从网站读取表格

webdata=pandas.read_html("https://book.douban.com/")

(6)导入文本数据

txtdata=pandas.read_table("G:/DA/product.txt")

2.matplotlib模块的使用

使用前提:

import matplotlib.pylab as py

import numpy as npy

(1)折线图/散点图:plot(x轴数据,y轴数据,展现形式<图形、颜色、线条形式>)

①折线图

x=[2,5,8,9,16]

y=[1,5,6,8,20]

py.plot(x,y)

py.show()

②散点图

x=[2,5,8,9,16]

y=[1,5,6,8,20]

py.plot(x,y,’o’)

③表头、x轴、y轴

py.title("show")

py.xlabel("x轴")

py.ylabel("y轴")

④x轴范围、y轴范围

py.xlim(0,20)

py.ylim(0,25)

⑤多组数据

x=[2,5,8,9,16]

y=[1,5,6,8,20]

py.plot(x,y)

x1=[1,5,12,20]

y1=[5,2,15,8]

py.plot(x1,y1)

py.show()

⑥颜色

>>>py.plot(x,y,'og')

ps:

c-cyan-青色

r-red-红色

m-magente-品红

g-green-绿色

b-blue-蓝色

y-yelow-黄色

k-black-黑色

w-white-白色

⑦线条形式

>>>py.plot(x,y,'-.')

ps:

-直线

--虚线

-. -.形式

:细小虚线

⑧点的形式

>>>py.plot(x,y,'*')

ps:

s-方形

h-六角形

H-六角形

*-*形

+-+形

x-x形

d-菱形

D-菱形

p-五角形

(2)直方图hist(某个数据出现的频数)

data=npy.random.normal(10.0,1.0,10000)

py.hist(data)

py.show()

ps:

data=npy.random.normal(10.0,1.0,10000)

style=npy.arange(1,16,2)

py.hist(data,style,histtype='stepfilled')

py.show()

【注】①style加上格式,histtype=‘stepfilled’取消格式;

②随机数生成,整数型random_integers(min,max,number)

import numpy as npy

ran1=npy.random.random_integers(1,20,10)

③随机生成正态分布的随机数random.normal(mean,方差,number)

ran2=npy.random.normal(5,0.9,10)

(3)绘制子图subplot(行,列,当前区域)

py.subplot(2,2,1)

x1=[1,2,3,4,5]

y1=[6,7,2,12,5]

py.plot(x1,y1)

py.subplot(2,2,2)

x2=[6,7,8,9,12]

y2=[0,9,3,15,16]

py.plot(x2,y2)

py.subplot(2,1,2)

x3=[3,5,9,12,16]

y3=[2,5,6,14,7]

py.plot(x3,y3)

py.show()

subplot子图绘制效果图

相关文章

网友评论

      本文标题:「数据分析」02数据源的导入与matplotlib模块的使用

      本文链接:https://www.haomeiwen.com/subject/inkpzctx.html