Seaborn countplot在泰坦尼克号数据上显示错误结果

2024-10-02 08:22:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在研究泰坦尼克号的数据集,我从这个网站上得到它: https://public.opendatasoft.com/explore/dataset/titanic-passengers/table/?flg=fr

我想显示每个幸存班级的男女人数(是或否)

首先,我得到了男性和女性的总数,使用:

bysex=data1['Sex'].value_counts()
print(bysex)

这给了我这些结果:

male      577
female    314
Name: Sex, dtype: int64

结果表明,男性人数多于女性

但当我使用seaborn来显示每个幸存类的男性和女性人数时,使用以下代码:

plot1 = sns.FacetGrid(data1, col='Survived')
plot1.map(sns.countplot,'Sex')

然后我得到这个结果: enter image description here

在这里,它表明女性的数量大于男性的数量,对于没有幸存的阶级来说,女性的数量(大约450人)甚至大于女性的总数(314人)

这怎么可能


Tags: 数据https数量网站public人数sns总数
1条回答
网友
1楼 · 发布于 2024-10-02 08:22:05

我认为地图有问题。 在左边的情节中,性别互换

data1.loc[data1["Survived"] == "No", 'Sex'].value_counts()
male      468
female     81
Name: Sex, dtype: int64

第二个情节是对的

data1.loc[data1["Survived"] == "Yes", 'Sex'].value_counts()
female    233
male      109
Name: Sex, dtype: int64

另一方面,当你使用

ax = sns.countplot(x="Survived", hue="Sex", data=data1)

你得到了正确的结果

enter image description here

相关问题 更多 >

    热门问题