unique()与nunique()
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()的更多相关文章
- 读书笔记6pandas简单使用
一.序列Series,很像numpy中的array数组,可以由列表.元组.字典.numpy中的array来初始化 >>> from pandas import Series > ...
- 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 ...
- Python--unique()与nunique()函数
参考:https://www.cnblogs.com/xxswkl/p/11009059.html 1 unique() 统计list中的不同值时,返回的是array.它有三个参数,可分别统计不同的量 ...
- [LeetCode] Unique Substrings in Wraparound String 封装字符串中的独特子字符串
Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...
- [LeetCode] Minimum Unique Word Abbreviation 最短的独一无二的单词缩写
A string such as "word" contains the following abbreviations: ["word", "1or ...
- [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- [LeetCode] Unique Word Abbreviation 独特的单词缩写
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
- [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
随机推荐
- 攻防世界--when_did_you_born5
测试文件:https://adworld.xctf.org.cn/media/task/attachments/24937e95ca4744818feebe82ab96902d 1.准备 root@l ...
- 【学习总结】快速上手Linux玩转典型应用-第3章-CentOS的安装
课程目录链接 快速上手Linux玩转典型应用-目录 目录 1. 虚拟机是什么 2. 在虚拟机中安装CentOS 3. 云服务器介绍 ================================== ...
- jquery.lazyload (JS懒加载框架使用详解)
/** 本地加载方式加载JS*/ NSString *path = [[NSBundle mainBundle] pathForResource:@"jquery.js&quo ...
- mac 命令行终端 设置代理
环境: macOS Mojave 10.14.3 iTrem 2 3.2.8 酸酸乳1.1.4.4-R8 查看自己命令行的状态 curl ip.gs 正式开始 一.首先检查自己的酸酸乳是否正常,并在高 ...
- 关于Mysql select语句中拼接字符串的记录
在mysql的SELECT语句中拼接两列(或多列)的字符串显示: mysql> select concat(dname,loc) from dept; 以上语句便把dept表的dname,loc ...
- vue + nginx部署404
记录: vue项目打包放到服务器,使用nginx反向代理的时候路由页面刷新报404,因为刷新时,不存在路径对应的文件或文件夹,需要在nginx配置中添加代码: index index.html; tr ...
- Thinkphp5 自定义分页样式显示页码和数量
Thinkphp5 自带的分页比较简单,本文通过修改Bootstrap类自定义显示分页的页码和数量 一.修改完成后如下图显示 二.修改Bootstrap代码: 1.为了不改动Bootstrap.php ...
- vue 之 双向绑定原理
一.实现双向绑定 详细版: 前端MVVM实现双向数据绑定的做法大致有如下三种: 1.发布者-订阅者模式(backbone.js) 思路:使用自定义的data属性在HTML代码中指明绑定.所有绑定起来的 ...
- php iconv函数转换出错问题
本人qq群也有许多的技术文档,希望可以为你提供一些帮助(非技术的勿加). QQ群: 281442983 (点击链接加入群:http://jq.qq.com/?_wv=1027&k=29Lo ...
- java tomcat虚拟目录的深入了解
我们知道,Web网站中的内容(包括网页,图片,音频文件等)一般都存放在App的目录下.但随着网站内容的不断丰富,用户需要把不同层次的内容组织成网站的子目录.我们通常的做法是在网站主目录下新建子目录,并 ...