Hello 大家好,我是一名新来的金融领域打工人,日常分享一些python知识,都是自己在学习生活中遇到的一些问题,分享给大家,希望对大家有一定的帮助!
在我前面的这篇文章中我有给大家分享如何查看DataFrame中的空值,具体的文章大家可以看这篇:。
当然采用我前面文章的方法可以比较定量地看出具体的空值数量,那么有没有一种可以通过可视化的方法来大概查看空值的分布呢?
最近我在做探索性数据分析的时候学到了一种通过可视化的方法查看空值分布的方法,下面我们就来介绍一下:
首先我们先读入数据,然后查看一些数据的基本性质:
import pandas as pd
df = pd.read_csv("train_friday.csv")
df
df.shape
## (550068, 12)
df.dtypes
### 结果
User_ID int64
Product_ID object
Gender object
Age object
Occupation int64
City_Category object
Stay_In_Current_City_Years object
Marital_Status int64
Product_Category_1 int64
Product_Category_2 float64
Product_Category_3 float64
Purchase int64
dtype: object
###
2.查看空值数量
可以看出这一份数据中有两列的数据的空值数量比较多:
df.isnull().sum() #空值计数
###
User_ID 0
Product_ID 0
Gender 0
Age 0
Occupation 0
City_Category 0
Stay_In_Current_City_Years 0
Marital_Status 0
Product_Category_1 0
Product_Category_2 173638
Product_Category_3 383247
Purchase 0
dtype: int64
###
查看空缺值的占比:
# 查看空缺值的占比
df.isnull().sum() / df.shape[0]
### 结果
User_ID 0.000000
Product_ID 0.000000
Gender 0.000000
Age 0.000000
Occupation 0.000000
City_Category 0.000000
Stay_In_Current_City_Years 0.000000
Marital_Status 0.000000
Product_Category_1 0.000000
Product_Category_2 0.315666
Product_Category_3 0.696727
Purchase 0.000000
dtype: float64
###
3.缺失值可视化
进行缺失值可视化的时候我们需要导入相关的包,具体代码如下:
## 导入缺失值可视化包
import missingno
# 查看缺失值的分布
missingno.matrix(df)
这样之后我们可以看看可视化的结果:
# 缺失值可视化
missingno.bar(df,color="blue")
我们来看看柱状图的形式:
其实上面这个图我们也可以看出空缺值较多的两列就是柱状图比较矮的那两列哦!
好啦,今天的知识就分享到这里啦!
因篇幅问题不能全部显示,请点此查看更多更全内容