返回分类序列的类别标签

2024-06-28 20:15:10 发布

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

我使用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]

我环顾四周,但找不到任何有用的东西。你知道吗

有什么帮助吗?你知道吗


Tags: csv数据代码noneirispandasreaddata
1条回答
网友
1楼 · 发布于 2024-06-28 20:15:10

data[4].cat.categories.values在这里可能比.unique()优越。你知道吗

看看https://pandas.pydata.org/pandas-docs/stable/categorical.html,“使用类别”。你知道吗

Note: The result of unique() is not always the same as Series.cat.categories, because Series.unique() has a couple of guarantees, namely that it returns categories in the order of appearance, and it only includes values that are actually present.

.cat.categories.values似乎快得多,因为我想上面列出的原因。你知道吗

示例:

import pandas as pd
import numpy as np

s = pd.Series(np.random.choice(['a', 'b', 'c'], 1000), dtype = "category")

% timeit a = s.unique()

# 303 µs ± 23.2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

% timeit b = s.cat.categories.values

# 1.26 µs ± 27.9 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

print(s.cat.categories.values)

# ['a' 'b' 'c']

数据集的大小和其他需求可能会决定哪个更好。你知道吗

相关问题 更多 >