我使用pandas提取数据集的类别,如以下代码所示:
data=pd.read_csv("iris.csv",header=None)
data[4]=data[4].astype("category")
当我打印数据[4]的值时,我得到了以下列表:
1 Setosa
2 Setosa
3 Setosa
4 Setosa
5 Setosa
6 Setosa
7 Setosa
...
149 Virginica
150 Virginica
Name: 4, Length: 150, dtype: category
Categories (3, object): [Setosa, Versicolor, Virginica]
但我想把这三个类别放在一个数组中,这样就有了:
[Setosa, Versicolor, Virginica]
我环顾四周,但找不到任何有用的东西。你知道吗
有什么帮助吗?你知道吗
data[4].cat.categories.values
在这里可能比.unique()
优越。你知道吗看看https://pandas.pydata.org/pandas-docs/stable/categorical.html,“使用类别”。你知道吗
.cat.categories.values
似乎快得多,因为我想上面列出的原因。你知道吗示例:
数据集的大小和其他需求可能会决定哪个更好。你知道吗
相关问题 更多 >
编程相关推荐