PART 1 知识点练习
一维数据分析
numpy
定义列表:
1)索引查询元素:
2)切片访问:
3)使用dtype
查看数据类型:
dtype详细信息参考网址:
Data type objects (docs.scipy.org
4)统计计算功能,如:平均值mean()、标准差std()
5)向量化运算,如:向量相加、向量乘以标量
pandas
定义一维数据结构:
1)获取描述统计信息:
2)获取值
iloc
属性用于根据位置顺序获取值、loc
属性用于根据索引获取值
3)向量化运算:向量相加
不同的向量相加时,索引不同会导致不相同的索引的数据为空值None
4)处理缺失值(空值)
方法2中,fill_valiue=0
将空值填充为0,去除缺失值,再将s2与s1相加
二维数据分析
numpy
二维数组结构
1)获取元素
2)按数轴进行计算
Pandas:数据框(DataFrame)
定义有序的数据框
1)平均值计算(按列计算)
2)获取值
iloc
属性用于根据位置顺序获取值
loc
属性用于根据索引获取值
3)复杂查询
切片功能:
条件判断:
简便写法:
4)查看数据集描述统计信息
PART 2 案例实践(朝阳医院2018年销售数据)
数据分析的基本步骤包括:
- 提出问题
- 理解数据
- 数据清洗
- 构建模型
- 数据可视化
1.提出问题
从销售数据中分析出以下业务指标: 1)月均消费次数 2)月均消费金额 3)客单价
2. 理解数据
2.1 读取Excel的数据
2.2 查看数据及字段
3. 数据清洗
3.1 选择子集
所有的列在数据分析中都需要用到,本案例不需要选择子集
3.2 列名重命名
3.3 缺失数据处理
python缺失值有3种:
1)Python内置的None值
2)在pandas中,将缺失值表示为NA,表示不可用not available。
3)对于数值数据,pandas使用浮点值NaN(Not a Number)表示缺失数据。
后面出来数据,如果遇到错误:说什么foloat错误,那就是有缺失值,需要处理掉
所以,缺失值有3种:None,NA,NaN
dropna函数详细使用地址:
pandas.DataFrame.dropna - pandas 1.1.2 documentationpandas.pydata.org
注:操作后发现没有变化,于是返回打印数据看是否有缺失值
可能原因是:显示看到的nan 实际就是个 'nan'的字符串 替换成NaN就可以删除。
3.4 数据类型转换
字符串转换为数值(浮点型)
字符串转换为日期数据类型
3.5 数据排序
by:按哪几列排序,ascending=True 表示升序排列,,ascending=True表示降序排列。
na_position=True表示排序的时候,把空值放到前列,这样可以比较清晰的看到哪些地方有空值。
官网文档:
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.sort_values.htmlpandas.pydata.org
重命名index
3.6 异常值处理
最小值中有负数,删除
4. 构建模型
4.1 求月均消费次数
月均消费次数=总消费次数 / 月份数
4.2 求月均消费金额
4.3 求客单价