1 unique()

统计list中的不同值时,返回的是array.它有三个参数,可分别统计不同的量,返回的都是array.

当list中的元素也是list时,尽量不要用这种方法.

import numpy as np
a = [1,5,4,2,3,3,5]
# 返回一个array
print(np.unique(a))
# 返回该元素在list中第一次出现的索引
print(np.unique(a,return_index=True))
# 返回原list中每个元素在新的list中对应的索引
print(np.unique(a,return_inverse=True))
# 返回该元素在list中出现的次数
print(np.unique(a,return_counts=True))
# 当加参数时,unique()返回的是一个tuple,这里利用了tuple的性质,即有多少个元素即可赋值给对应的多少个变量
p,q,m,n = np.unique(a,return_index=True,return_inverse=True,return_counts=True)
print(p,q,m,n)
# [1 2 3 4 5]
# (array([1, 2, 3, 4, 5]), array([0, 3, 4, 2, 1]))
# (array([1, 2, 3, 4, 5]), array([0, 4, 3, 1, 2, 2, 4]))
# (array([1, 2, 3, 4, 5]), array([1, 1, 2, 1, 2]))
# [1 2 3 4 5] [0 3 4 2 1] [0 4 3 1 2 2 4] [1 1 2 1 2] # 注意当list中的元素不是数字而是list的时候,输出的数据类型与list中元素的长度有关
# 利用这种方法对list中元素去重或求里面元素的个数都不是好方法,很容易出错
b = [[1,2],[3,4]]
print(np.unique(b))
# b中有两个list,输出的却是4
print(len(np.unique(b)))
c = [[1,2],[3,4],[5]]
print(np.unique(c))
print(len(np.unique(c)))
# [1 2 3 4]
#
# [list([1, 2]) list([3, 4]) list([5])]
#

统计series中的不同值时,返回的是array,它没有其它参数

import pandas as pd
se = pd.Series([1,3,4,5,2,2,3])
print(se.unique())
# [1 3 4 5 2]

2 nunique()

可直接统计dataframe中每列的不同值的个数,也可用于series,但不能用于list.返回的是不同值的个数.

df=pd.DataFrame({'A':[0,1,1],'B':[0,5,6]})
print(df)
print(df.nunique())
# A B
# 0 0 0
# 1 1 5
# 2 1 6
# A 2
# B 3
# dtype: int64

也可与groupby结合使用,统计每个块的不同值的个数.

all_user_repay = all_user_repay.groupby(['user_id'])['listing_id'].agg(['nunique']).reset_index()
# user_id nunique
# 0 40 1
# 1 56 1
# 2 98 1
# 3 103 1
# 4 122 1

unique()与nunique()的更多相关文章

  1. 读书笔记6pandas简单使用

    一.序列Series,很像numpy中的array数组,可以由列表.元组.字典.numpy中的array来初始化 >>> from pandas import Series > ...

  2. LeetCode: Unique Paths I & II & Minimum Path Sum

    Title: https://leetcode.com/problems/unique-paths/ A robot is located at the top-left corner of a m  ...

  3. Python--unique()与nunique()函数

    参考:https://www.cnblogs.com/xxswkl/p/11009059.html 1 unique() 统计list中的不同值时,返回的是array.它有三个参数,可分别统计不同的量 ...

  4. [LeetCode] Unique Substrings in Wraparound String 封装字符串中的独特子字符串

    Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...

  5. [LeetCode] Minimum Unique Word Abbreviation 最短的独一无二的单词缩写

    A string such as "word" contains the following abbreviations: ["word", "1or ...

  6. [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...

  7. [LeetCode] Unique Word Abbreviation 独特的单词缩写

    An abbreviation of a word follows the form <first letter><number><last letter>. Be ...

  8. [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  9. [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

随机推荐

  1. String.Net “System.TypeInitializationException”类型的未经处理的异常在 Spring.NetDemo.exe 中发生

    今天编写String.Net时,遇到“System.TypeInitializationException”类型的未经处理的异常在 Spring.NetDemo.exe 中发生 原因配置文件的顺序写错 ...

  2. docker elk

    1.核心组成 ELK由Elasticsearch.Logstash和Kibana三部分组件组成: Elasticsearch是个开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现,索引自动分片, ...

  3. oracle数据库ID自增长--序列

    什么是序列?在mysql中有一个主键自动增长的id,例如:uid number primary key auto_increment;在oracle中序列就是类似于主键自动增长,两者功能是一样的,只是 ...

  4. 用eclipse怎么打war包?

    用eclipse怎么打war包? 在服务器上部署很多都是用war包进行部署的,eclipse是很友好的支持把java项目打成war包的,下面就把打war的经验写出来,供大家参考 百度经验:jingya ...

  5. Matlab 2019a 下载和安装步骤

    目录 1. 安装包下载 + 多套数学建模视频教程 2. 安装步骤 1. 安装包下载 + 多套数学建模视频教程 参考:https://blog.csdn.net/COCO56/article/detai ...

  6. makeinfo - 翻译 Texinfo 文档

    SYNOPSIS 总览 makeinfo [OPTION]... TEXINFO-FILE... DESCRIPTION 描述 将 Texinfo 源文档翻译为各种其他格式,默认是可以用 Emacs ...

  7. Comet OJ - Contest #2 B 她的想法、他的战斗(概率 + 数学)

    题目描述 Takuru 是一名情报强者,所以他想利用他强大的情报搜集能力来当中间商赚差价. Takuru 的计划是让 Hinae 帮他去市场上买一个商品,然后再以另一个价格卖掉它.Takuru 会给 ...

  8. CF 937

    A #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #def ...

  9. DOM-document 对象

    Document 对象Document 对象代表整个HTML 文档,可用来访问页面中的所有元素.Document 对象是 Window 对象的一个部分,可通过 window.document 属性来访 ...

  10. Android PdfViewer案例使用

    今天按项目要求找了一个android的PDF控件,各种操作效果都非常好,在这里和大家分享一下. com.joanzapata.pdfview:android-pdfview  该PDF控件加载大存储的 ...