python - 虹膜数据集未显示 "Species"列

标签 python pandas dataframe dataset iris-dataset

我正在使用 Python 上的 numpy 和 pandas 来学习如何处理数据帧。

我在 Collaboratory 上编码,我已经加载了鸢尾花数据集,但出于某种原因,我的数据框中没有“物种”列。也许我以不正确的方式加载它?我很感激在这件事上的帮助。

我添加了一张图片,如果仍然需要代码,那么这就是我所拥有的:

import numpy as np
import pandas as pd
from sklearn.datasets import load_iris

df = pd.DataFrame(load_iris().data, columns=load_iris().feature_names)

enter image description here

最佳答案

尝试:

import numpy as np
import pandas as pd 
from sklearn.datasets import load_iris

iris = load_iris()

df = pd.DataFrame(data=np.c_[iris['data'], iris['target']],
                  columns= iris['feature_names'] + ['target']).astype({'target': int}) \
       .assign(species=lambda x: x['target'].map(dict(enumerate(iris['target_names']))))

输出:

>>> df
     sepal length (cm)  sepal width (cm)  petal length (cm)  petal width (cm)  target    species
0                  5.1               3.5                1.4               0.2       0     setosa
1                  4.9               3.0                1.4               0.2       0     setosa
2                  4.7               3.2                1.3               0.2       0     setosa
3                  4.6               3.1                1.5               0.2       0     setosa
4                  5.0               3.6                1.4               0.2       0     setosa
..                 ...               ...                ...               ...     ...        ...
145                6.7               3.0                5.2               2.3       2  virginica
146                6.3               2.5                5.0               1.9       2  virginica
147                6.5               3.0                5.2               2.0       2  virginica
148                6.2               3.4                5.4               2.3       2  virginica
149                5.9               3.0                5.1               1.8       2  virginica

[150 rows x 6 columns]

如何从 targettarget_names 列创建 species 列?

>>> iris['target_names']
array(['setosa', 'versicolor', 'virginica'], dtype='<U10')
# index 0: setosa
# index 1: versicolor
# index 2: virginica

>>> iris['target']
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])

您只需要一个字典映射来将 0 替换为“setosa”,将 1 替换为“versicolor”,将 2 替换为“virginica”。使用 enumerate 创建一个元组列表 [(0, 'setosa'), (1, 'versicolor'), (2, 'virginica')] 然后 dict` 到转换为字典:

>>> dict(enumerate(iris['target_names']))
{0: 'setosa', 1: 'versicolor', 2: 'virginica'}

现在Series.map会映射对应的值:

>>> df['target'].map(dict(enumerate(iris['target_names'])))
0         setosa
1         setosa
2         setosa
3         setosa
4         setosa
         ...    
145    virginica
146    virginica
147    virginica
148    virginica
149    virginica
Name: target, Length: 150, dtype: object

关于python - 虹膜数据集未显示 "Species"列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69821857/

相关文章:

python - 将组合结果分组到带有子列表的列表中

python - 过滤多年的 Pandas

python - 如何将特定dtype对象列的字段转换为pandas中数据框的列

python - key 错误:('1', 'occurred at index 0')

python - 在python中将列表列表转换为数据框

python - 如何在 python 2.7 中从邮件的有效负载中仅打印消息正文

python - 解析具有未定义实体的 XHTML5

python - Windows 上程序的 CPU 使用率测量

python - 如何将 pandas.to_datetime 与 "strange"字符串格式一起使用

python - 基于索引的 Pandas Dataframe Mask