数据分析的一般流程
1. 提取数据(筛选)
2. 整合和重塑
3. 数据清洗(缺失值、空值、异常值、格式、量纲、名称)
-
缺失值的处理:
- dropna() 删除空值
- fillna() 填充空值<— 均值、中位数、众数
- isnull() / notnull() 判断空值
-
重复值的处理:
- duplicated(): 判断是否重复
- drop_duplicates(): 删除重复值
-
异常值的处理:
- 异常值 / 极端值 / 离群值 —> 1.5倍IQR / 3σ法则
极端值和离群值会在下次或之后复现 - drop(): 删除
- replace():替换
- 异常值 / 极端值 / 离群值 —> 1.5倍IQR / 3σ法则
-
预处理:
-
apply():将函数作用到数据上.需要指定在哪个轴上运行
-
transform():将(多个)函数作用到数据上
transform可以传两个函数,会变成二级索引transform不能使用归约函数
-
applymap():对每个元素使用指定的函数进行映射,相当于Series上的map()方法—>elementwise
-
注意:这三个方法都是高阶函数的用法,apply()方法接收的函数可以是带归约性质的函数(聚合函数),也可以是没有归约性质的函数。transform()方法接受的函数不能是带归约功能的函数,只能是对数据做变换的函数,简单地说就是函数的执行不会减少数据的量。
-
4. 数据透视(排序、分组、聚合)
5. 数据可视化(统计图表)
6. 数据的洞察(发现问题给出建议) —>一般性的方法(数据思维)和经验的积累
规约函数:会把数据汇合,np.sum()
连接数据库
import pymysql
conn = pymysql.connect(host='47.104.31.138',port=3306,
user='guest',password='Guest.618',
database='hrs',charset='utf8mb4'
)
dept_df=pd.read_sql('select dno,dname,dloc from tb_dept',conn)
emp_df=pd.read_sql('select eno,ename,job,mgr,sal,comm,dno from tb_emp',
con=conn,
# index_col='eno'
)
连接表
# 连接两张表
# on:基于哪个连接
# 左右内外连接
pd.merge(emp_df,dept_df,on='dno',how='inner').set_index('eno')
# 连表法二
emp_df.merge(dept_df,how='inner',on='dno').set_index('eno')
数据查询
# 方式一
emp_df[(emp_df.dno ==20) & (emp_df.sal>=5000)]
# 方式二
emp_df.query('dno==20 and sal>=5000')
删除不满足的条件
# 删掉不满足的条件并返回新列表
emp_df.drop(emp_df[emp_df.dno != 20].index)
# emp_df.drop(emp_df[emp_df.dno != 20].index,inplace=True)
# inplace=True:把不满足条件的删掉,且返回原列表
# 删行index,删列columns
emp_df.drop(columns=['mgr','dno'],inplace=True)
emp_df
修改索引
# 修改行索引或者列索引
emp_df.rename(columns={'sal':'salary','comm':'allowance'},inplace=True)
emp_df
重置索引
# 重置索引,返回新对象
# emp_df.reset_index()
emp_df.reset_index(inplace=True)
#在原列表上修改
# 不能多次在原列表上进行修改
设置索引
# 设置索引:指定一个列或多个列充当索引
# 多级索引
# emp_df.set_index(['eno','ename'])
# 单级索引
emp_df.set_index('eno',inplace=True)
多级索引
tmp = pd.DataFrame({'A':range(3),'B':range(1,4)})
# 在列上形成了多级索引
tmp = tmp.transform([np.sqrt,np.exp])
tmp
调整索引
emp_df.reindex(columns=['salary','allowance','job'])
# 列调整索引,调整索引顺序
调整索引顺序,类似花式索引
emp_df.reindex(index=['李莫愁','张三丰','乔峰'])
选择性删除某些空值的列
# 删除某些空值的列,保留某些,可以用series来处理
mgr = emp_df.mgr
tmp = emp_df.dropna(axis=1)
tmp['mgr']=mgr
tmp
数据处理
import itertools
names = ['高新','新津','犀浦']
years=['2017','2018','2019']
# 生成笛卡尔积
for name,year in itertools.product(names,years):
print(name)
import itertools
names = ['高新','新津','犀浦']
years=['2018','2019']
dfs = [pd.read_excel(f'data/小宝剑大药房({name}店){year}年销售数据.xlsx',header=1)
for name,year in itertools.product(names,years)
]
pd.concat(dfs,ignore_index=True).to_excel('小宝剑大药房2018-2019年销售数据汇总.xlsx')
youtube为例
youtube_df = pd.read_excel(
f'data/某视频网站运营数据.xlsx',
usecols = ['video_id','title','channel_title','views','likes']
)
# 显示前10行
youtube_df.head(10)
# 显示后10行
youtube_df.tail(10)
# 判断重复
youtube_df.duplicated('video_id')
# 去重
youtube_df.drop_duplicates('video_id')
# keep = first last False
# 遇到相同保留第一个、最后一个、一个都不保留
emp_df.replace('程序员','程序猿')
# replace满足正则表达式
emp_df.at[2,'job'] = '程序媛'
emp_df
# 使用正则表达式的使用
emp_df.replace('程序[猿媛]','程序员',regex=True)
emp_df['job']=emp_df.job.str.replace('程序[媛猿]','程序员')
emp_df
# 修改数据
# 直接作用到每个元素上,只对每个元素进行操作
tmp.applymap(lambda x:x**2)
# 归约
tmp1 = pd.DataFrame({'A':range(3),'B':range(1,4)})
tmp1.apply(np.sum,axis=1)
# 没有归约
tmp1.apply(np.sqrt)