在Pandas中使用DataFrame时,您需要找到列中存在的 unique 元素。为此,无涯教程必须使用unique()方法从列中提取唯一值。 Python中的Pandas库可以轻松地帮助找到唯一的数据。
列中存在的唯一值按其出现的顺序返回。这不会排序其出现顺序。此外,此方法基于哈希表(hash-table)。
它比 numpy.unique()方法要快得多,并且还包含空值。
pandas.unique(values)
values - 它是指由数组值组成的一维类似于数组的对象。
此方法返回numpy.ndarray或ExtensionArray对象,可以是:
import pandas as pd pd.unique(pd.Series([2, 1, 3, 3])) pd.unique(pd.Series([pd.Timestamp('20160101'), pd.Timestamp('20160101')]))
输出:
array(['2016-01-01T00:00:00.000000000'], dtype='datetime64[ns]')
示例2:下面的示例从Index中提取唯一的时间戳:
import pandas as pd import numpy as np pd.unique(pd.Index([pd.Timestamp('20160101', tz='US/Eastern'), pd.Timestamp('20160101', tz='US/Eastern')]))
输出:
DatetimeIndex(['2016-01-01 00:00:00-05:00'], dtype='datetime64[ns, US/Eastern]', freq=None)
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)
Spring Boot与Kubernetes云原生微服务实践 -〔杨波〕