| pandas.unique | CategoricalIndex.unique | Series.unique : Return unique values of Series object.
时间: 2024-05-30 19:09:33 浏览: 10
`pandas.unique` and `Series.unique` are similar functions that return the unique values of a pandas Series object. The main difference is that `pandas.unique` can also be used with other pandas data structures like DataFrames and arrays, while `Series.unique` is specific to Series objects.
On the other hand, `CategoricalIndex.unique` returns the unique values of a pandas CategoricalIndex object, which is a specialized index type that allows for efficient storage and manipulation of categorical data.
In summary:
- Use `pandas.unique` or `Series.unique` to get the unique values of a Series object.
- Use `CategoricalIndex.unique` to get the unique values of a CategoricalIndex object.
相关问题
pd.Series.nunique
pd.Series.nunique 是一个 Pandas 库中 Series 对象的方法,用于计算 Series 中唯一值的数量。它返回 Series 中不同值的个数。
例如,假设我们有以下 Series 对象:
```
import pandas as pd
s = pd.Series([1, 2, 3, 1, 2, 3, 4, 5])
```
我们可以使用 nunique 方法来计算 Series 中的唯一值数量:
```
unique_values = s.nunique()
print(unique_values)
```
输出结果为 5,因为 Series 中有 1、2、3、4 和 5 这五个不同的值。
希望这个解答能够帮助到你!如果你还有其他问题,请随时提问。
pd.Series.nunique 删除空值
抱歉,我之前的回答有误。pd.Series.nunique 方法默认情况下不会删除空值,它会将空值作为一个独立的类别进行计数。
如果你想排除空值并计算唯一值的数量,可以使用 dropna 参数来过滤掉空值。将 dropna 参数设置为 True,即可在计算唯一值数量时忽略空值。
以下是示例代码:
```python
import pandas as pd
s = pd.Series([1, 2, 3, 1, 2, 3, 4, 5, None])
unique_values = s.dropna().nunique()
print(unique_values)
```
在上述示例中,Series 对象 s 中包含一个空值 None。通过使用 dropna 方法先删除空值,然后再调用 nunique 方法计算唯一值数量,最终输出结果为 5,排除了空值。
非常抱歉之前的错误回答带来的困惑。如果还有其他问题,请随时提问。
相关推荐
![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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)