plt.boxplot缺失值
时间: 2023-10-30 10:58:43 浏览: 36
plt.boxplot函数在处理缺失值时的默认行为是忽略缺失值并正常绘制箱线图。即使数据中存在缺失值,plt.boxplot函数仍会绘制出箱线图。 如果你想在箱线图中显示缺失值,你可以先通过pandas库或其他方法对数据进行处理,将缺失值替换为特定的数值,然后再传递给plt.boxplot函数进行绘制。
相关问题
import numpy as np import pandas as pd import seaborn as sns import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus']=False %matplotlib inline pdtitanic = pd.read_csv(r"C:\Users\小杜羽\Desktop\titanic.csv") pdtitanic.head() pdtitanic.isnull().sum() mean=titanic['age'].mean() print(mean) pdtitanic['age']=['age'].fillna(mean) pdtitanic.isnull().sum() sns.distplot(titanic["age"]) titanic["embarked"],value_counts() del titanic.['deck'] titanic.head sns.countplot(x="sex",data=titanic) sns.boxplot(x="sex",y="age",data=titanic) sns.countplot(x="class",data=titanic) sns.violinplot(y="age",x="class",data=titanic) def agelevel(age): if age<=16: return'child' elif age>=60: return'old' else: return'middle' titanic['age_level']=titanic['age'].map(agelevel) titanic.head sns.countplot(x='age_level',data=titanic) sns.countplot(x='alive',hue='age_level',data=titanic) plt.legend(loc='best',fontsize='15')
你的代码中有一些错误,我来帮你看看:
1. 在导入数据时,你使用了`pdtitanic`变量,但是后面的代码中却使用了`titanic`变量,需要统一变量名。
2. 在填充年龄缺失值时,使用了字符串而不是变量名,应该改为`pdtitanic['age']=pdtitanic['age'].fillna(mean)`。
3. 在查看`embarked`列的取值时,应该使用`pdtitanic["embarked"].value_counts()`。
4. 在删除`deck`列时,需要在`del`和列名之间加上一个".",即`del titanic['deck']`。
5. 在查看`age_level`列的取值时,需要在`titanic.head`后面加上一个括号,即`titanic.head()`。
6. 在画`countplot`时,需要在`plt.legend`前面加上`sns.`,即`sns.countplot(x='alive',hue='age_level',data=titanic) plt.legend(loc='best',fontsize='15')`。
修改后的代码如下所示:
```python
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus']=False
%matplotlib inline
titanic = pd.read_csv(r"C:\Users\小杜羽\Desktop\titanic.csv")
titanic.head()
titanic.isnull().sum()
mean = titanic['age'].mean()
print(mean)
titanic['age'] = titanic['age'].fillna(mean)
titanic.isnull().sum()
sns.distplot(titanic["age"])
print(titanic["embarked"].value_counts())
del titanic['deck']
sns.countplot(x="sex",data=titanic)
sns.boxplot(x="sex",y="age",data=titanic)
sns.countplot(x="class",data=titanic)
sns.violinplot(y="age",x="class",data=titanic)
def agelevel(age):
if age<=16:
return 'child'
elif age>=60:
return 'old'
else:
return 'middle'
titanic['age_level'] = titanic['age'].map(agelevel)
titanic.head()
sns.countplot(x='age_level',data=titanic)
sns.countplot(x='alive',hue='age_level',data=titanic)
sns.legend(loc='best',fontsize='15')
```
根据某餐厅日常销售数据(catering_sale.xls)进行数据质量分析, 缺失值过滤,过滤掉含有缺失值记录,异常检测,绘制箱图
好的,让我来回答你的问题。
首先,对于缺失值过滤,我们可以使用 Pandas 库中的 dropna() 函数。具体代码如下:
```python
import pandas as pd
data = pd.read_excel('catering_sale.xls')
data.dropna(inplace=True)
```
这里我们使用了 inplace 参数来直接在原始数据上进行修改。
接下来是异常检测。我们可以使用箱线图来检测异常值。箱线图是一种常用的统计图形,可以用于显示一组数据的中位数、四分位数、极值以及异常值。
绘制箱线图,我们可以使用 Matplotlib 库。具体代码如下:
```python
import matplotlib.pyplot as plt
plt.boxplot(data.values, sym='o', whis=1.5)
plt.show()
```
这里我们使用了 sym 参数来指定异常值的标记,使用 whis 参数来指定箱线图的上下边界。
综上,完整代码如下:
```python
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_excel('catering_sale.xls')
data.dropna(inplace=True)
plt.boxplot(data.values, sym='o', whis=1.5)
plt.show()
```
希望这个回答能够帮助到你!
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)