本文为记录学习用python数据分析包:numpy 和 pandas 的过程,总结来说学习过程总体较为顺畅,因基础比较巩固,大部分代码原理能够理解,后续就是多打代码,多运用,熟悉各种用法,把学习到的东西化为己用。
【主要内容】:
1.一维数据分析
2.二维数据分析
3.通过案例展示数据分析的步骤
【可用参考资料】:
(1)Numpy:
Quickstart tutorialdocs.scipy.org![91530309f26a083fec655fb8a938003f.png](https://i-blog.csdnimg.cn/blog_migrate/15779680dc06944e083f86ca4d3b718d.png)
(2)Pandas:
10 minutes to pandaspandas.pydata.org![095d7606c07815f39fbd8b6549ea0c2b.png](https://i-blog.csdnimg.cn/blog_migrate/03f1f342d188567869ef512ce7656530.png)
(一)一维数据分析
![4efecbb5bb8392ac5fd8c761ca86080f.png](https://i-blog.csdnimg.cn/blog_migrate/087952fd1d7d6849ae98c39eea48e1bd.jpeg)
#导入numpy包
NumPy 一维数组
#定义一组数组array
一维数据分析:Pandas
#定义:Pandas一维数据结构:Series
(二)二维数据分析
![3a4cee0b6d2cc126062f376d6c382a33.png](https://i-blog.csdnimg.cn/blog_migrate/f8fa6fe35121226cb052a5ce8c08841a.jpeg)
#导入包
二维数据分析:NumPy
'''
Numpy数轴参数:axis
#如果没有指定数轴参数,会计算整个数组的平均值
Pandas二维数组:数据框(DataFrame)
定义数据框
'''
购药时间 社保卡号 商品编码 商品名称 销售数量 应收金额 实收金额 0 2018-01-01 星期五 001616528 236701 强力VC银翘片 6 82.8 69.00 1 2018-01-02 星期六 001616528 236701 清热解毒口服液 1 28.0 24.64 2 2018-01-06 星期三 0012602828 236701 感康 2 16.8 15.00
#平均值:是按每列来求平均值
查询数据:iloc属性用于根据位置获取值
'''
查询数据:loc属性用于根据索引获取值
'''
数据框复杂查询:切片功能
#通过列表来选择某几列的数据
商品名称 销售数量 0 强力VC银翘片 6 1 清热解毒口服液 1 2 感康 2
#通过切片功能,获取指定范围的列
购药时间 社保卡号 商品编码 商品名称 销售数量 0 2018-01-01 星期五 001616528 236701 强力VC银翘片 6 1 2018-01-02 星期六 001616528 236701 清热解毒口服液 1 2 2018-01-06 星期三 0012602828 236701 感康 2
数据框复杂查询:条件判断
#通过条件判断筛选
购药时间 社保卡号 商品编码 商品名称 销售数量 应收金额 实收金额 0 2018-01-01 星期五 001616528 236701 强力VC银翘片 6 82.8 69.0 2 2018-01-06 星期三 0012602828 236701 感康 2 16.8 15.0
查看数据集描述统计信息
#读取Ecxcel数据
购药时间 社保卡号 商品编码 商品名称 销售数量 应收金额 实收金额 0 2018-01-01 星期五 1616528.0 236701.0 强力VC银翘片 6.0 82.8 69.00 1 2018-01-02 星期六 1616528.0 236701.0 清热解毒口服液 1.0 28.0 24.64 2 2018-01-06 星期三 12602828.0 236701.0 感康 2.0 16.8 15.00
#有多少行,多少列
(三)数据分析的步骤
![8277d48650640045b7ac839a42200113.png](https://i-blog.csdnimg.cn/blog_migrate/d31fafe07e2fd5e370cbb5e1429ab98a.jpeg)
#import the data analysis package(导入数据分析包)
1.Ask questions(提出问题)
【1】月均消费次数 【2】月均消费金额 【3】客单价 【4】消费趋势
2.Understand the data(理解数据)
【1】Read Excel data(读取Excel数据)
#Read Excel data,read in according to STR first,then convert(读取Excel数据,统一先按照str读入,之后转换)
【2】Print the first few lines(打印前几行):saleDff.head()
#Print out the first 3 lines to make sure the data is running properly(打印出前3行,以确保数据运行正常)
购药时间 社保卡号 商品编码 商品名称 销售数量 应收金额 实收金额 0 2018-01-01 星期五 001616528 236701 强力VC银翘片 6 82.8 69 1 2018-01-02 星期六 001616528 236701 清热解毒口服液 1 28 24.64 2 2018-01-06 星期三 0012602828 236701 感康 2 16.8 15
【3】How many rows,how many columns(有多少行多少列)
salesDf
【4】View the data type of the column(查看列的数据类型)
salesDf
3.Data cleaning(数据清洗)
【1】select a subset(选择子集)
#There is no need to select a subset in this case(本案例不需要选择子集)
购药时间 社保卡号 商品编码 商品名称 销售数量 0 2018-01-01 星期五 001616528 236701 强力VC银翘片 6 1 2018-01-02 星期六 001616528 236701 清热解毒口服液 1 2 2018-01-06 星期三 0012602828 236701 感康 2 3 2018-01-11 星期一 0010070343428 236701 三九感冒灵 1 4 2018-01-15 星期五 00101554328 236701 三九感冒灵 8
【2】column to rename(列重命名)
#Dictionary, correspondence between old column names and new column names(字典,旧列名和新列名的对应关系)
销售时间 社保卡号 商品编码 商品名称 销售数量 应收金额 实收金额 0 2018-01-01 星期五 001616528 236701 强力VC银翘片 6 82.8 69 1 2018-01-02 星期六 001616528 236701 清热解毒口服液 1 28 24.64 2 2018-01-06 星期三 0012602828 236701 感康 2 16.8 15
【3】missing data processing(缺失数据处理)
print
【4】Conversion of data types(数据类型的转换)
(1)The string is converted to a floating point number(浮点数)
salesDf
(2)Processing date(处理日期)
#String spliting (字符串分割)
销售时间 社保卡号 商品编码 商品名称 销售数量 应收金额 实收金额 0 2018-01-01 001616528 236701 强力VC银翘片 6.0 82.8 69.00 1 2018-01-02 001616528 236701 清热解毒口服液 1.0 28.0 24.64 2 2018-01-06 0012602828 236701 感康 2.0 16.8 15.00
(3)String conversion date (字符串转换日期)
# errors= 'coerce',如果原始数据不符合日期格式,转换后的值为NaT
【5】Sort(排序)
'''
销售时间 社保卡号 商品编码 商品名称 销售数量 应收金额 实收金额 0 2018-01-01 001616528 236701 强力VC银翘片 6.0 82.8 69.0 2698 2018-01-01 0013448228 861507 苯磺酸氨氯地平片(安内真) 1.0 9.5 8.5 264 2018-01-01 00101470528 236709 心痛定 4.0 179.2 159.2
Rename the row name(重命名行名)
salesDf
销售时间 社保卡号 商品编码 商品名称 销售数量 应收金额 实收金额 0 2018-01-01 001616528 236701 强力VC银翘片 6.0 82.8 69.0 1 2018-01-01 0013448228 861507 苯磺酸氨氯地平片(安内真) 1.0 9.5 8.5 2 2018-01-01 00101470528 236709 心痛定 4.0 179.2 159.2
【6】异常值处理
salesDf
销售数量 应收金额 实收金额 count 6549.000000 6549.000000 6549.000000 mean 2.384486 50.449076 46.284370 std 2.375227 87.696401 81.058426 min -10.000000 -374.000000 -374.000000 25% 1.000000 14.000000 12.320000 50% 2.000000 28.000000 26.500000 75% 2.000000 59.600000 53.000000 max 50.000000 2950.000000 2650.000000
The data was screened through conditional judgment(通过条件判断筛选出数据)
#remove outliers:The data was screened through conditional judgment(删除异常值:通过条件判断筛选出数据)
Business indicator 1 : Monthly average consumption times(业务指标1:月均消费次数)
Monthly consumption times = Total consumption times/Monthly number
ALL consumption by the same person on the same day counts as one consumption
#Total consumption times (总消费次数)
Business indicator 2:monthly consumption (业务指标2:月均消费金额)
Total consumption / Monthly number(总消费金额/月份数)
#Total consumption(总消费金额)
Business indicator 3:unit price(业务指标3:客单价)
Total consumption /Total consumption times(总消费金额/总消费次数)
'''