pandas的corsstab
pandas.crosstab(index, columns, values=None, rownames=None, colnames=None, aggfunc=None, margins=False, dropna=True, normalize=False)
index : array-like, Series, or list of arrays/Series
Values to group by in the rows
columns : array-like, Series, or list of arrays/Series
Values to group by in the columns
values : array-like, optional
Array of values to aggregate according to the factors. Requires aggfunc be specified.
aggfunc : function, optional
If specified, requires values be specified as well
rownames : sequence, default None
If passed, must match number of row arrays passed
colnames : sequence, default None
If passed, must match number of column arrays passed
margins : boolean, default False
Add row/column margins (subtotals)
dropna : boolean, default True
Do not include columns whose entries are all NaN
normalize : boolean, {‘all’, ‘index’, ‘columns’}, or {0,1}, default False
Normalize by dividing all values by the sum of values.
- If passed ‘all’ or True, will normalize over all values.
- If passed ‘index’ will normalize over each row.
- If passed ‘columns’ will normalize over each column.
- If margins is True, will also normalize margin values.
New in version 0.18.1.
import numpy as np
a = np.array(["foo", "foo", "foo", "foo", "bar", "bar","bar", "bar", "foo", "foo", "foo"], dtype=object)
a
b = np.array(["one", "one", "one", "two", "one", "one", "one", "two", "two", "two", "one"], dtype=object)
b
pd.crosstab(a,b)
| col_0 | one | two |
|---|---|---|
| row_0 | ||
| bar | 3 | 1 |
| foo | 4 | 3 |
pd.crosstab(a, b, rownames=['a'], colnames=['b'])
| b | one | two |
|---|---|---|
| a | ||
| bar | 3 | 1 |
| foo | 4 | 3 |
c = np.array(["dull", "dull", "shiny", "dull", "dull", "shiny","shiny", "dull", "shiny", "shiny", "shiny"],
dtype=object)
c
import pandas as pd
pd.crosstab(a, [b, c], rownames=['a'], colnames=['b', 'c'])
| b | one | two | ||
|---|---|---|---|---|
| c | dull | shiny | dull | shiny |
| a | ||||
| bar | 1 | 2 | 1 | 0 |
| foo | 2 | 2 | 1 | 2 |
foo1 = pd.Categorical(['a', 'b'], categories=['a', 'b', 'c'])
bar1= pd.Categorical(['d', 'e'], categories=['d', 'e', 'f'])
pd.crosstab(foo1, bar1,dropna='true')
# 'c' and 'f' are not represented in the data,
# and will not be shown in the output because
# dropna is True by default. Set 'dropna=False'
# to preserve categories with no data
| col_0 | d | e | f |
|---|---|---|---|
| row_0 | |||
| a | 1 | 0 | 0 |
| b | 0 | 1 | 0 |
| c | 0 | 0 | 0 |
pandas的corsstab的更多相关文章
- pandas基础-Python3
未完 for examples: example 1: # Code based on Python 3.x # _*_ coding: utf-8 _*_ # __Author: "LEM ...
- 10 Minutes to pandas
摘要 一.创建对象 二.查看数据 三.选择和设置 四.缺失值处理 五.相关操作 六.聚合 七.重排(Reshaping) 八.时间序列 九.Categorical类型 十.画图 十一 ...
- 利用Python进行数据分析(15) pandas基础: 字符串操作
字符串对象方法 split()方法拆分字符串: strip()方法去掉空白符和换行符: split()结合strip()使用: "+"符号可以将多个字符串连接起来: join( ...
- 利用Python进行数据分析(10) pandas基础: 处理缺失数据
数据不完整在数据分析的过程中很常见. pandas使用浮点值NaN表示浮点和非浮点数组里的缺失数据. pandas使用isnull()和notnull()函数来判断缺失情况. 对于缺失数据一般处理 ...
- 利用Python进行数据分析(12) pandas基础: 数据合并
pandas 提供了三种主要方法可以对数据进行合并: pandas.merge()方法:数据库风格的合并: pandas.concat()方法:轴向连接,即沿着一条轴将多个对象堆叠到一起: 实例方法c ...
- 利用Python进行数据分析(9) pandas基础: 汇总统计和计算
pandas 对象拥有一些常用的数学和统计方法. 例如,sum() 方法,进行列小计: sum() 方法传入 axis=1 指定为横向汇总,即行小计: idxmax() 获取最大值对应的索 ...
- 利用Python进行数据分析(8) pandas基础: Series和DataFrame的基本操作
一.reindex() 方法:重新索引 针对 Series 重新索引指的是根据index参数重新进行排序. 如果传入的索引值在数据里不存在,则不会报错,而是添加缺失值的新行. 不想用缺失值,可以用 ...
- 利用Python进行数据分析(7) pandas基础: Series和DataFrame的简单介绍
一.pandas 是什么 pandas 是基于 NumPy 的一个 Python 数据分析包,主要目的是为了数据分析.它提供了大量高级的数据结构和对数据处理的方法. pandas 有两个主要的数据结构 ...
- pandas.DataFrame对行和列求和及添加新行和列
导入模块: from pandas import DataFrame import pandas as pd import numpy as np 生成DataFrame数据 df = DataFra ...
随机推荐
- 网易云课堂_C++程序设计入门(下)_第11单元:工欲善其事必先利其器 - STL简介_第11单元 - 单元作业2:OJ编程 - list 与 deque
第11单元 - 单元作业2:OJ编程 - list 与 deque 查看帮助 返回 温馨提示: 1.本次作业属于Online Judge题目,提交后由系统即时判分. 2.学生可以在作业截止时间之 ...
- 原生dapper中新增用户后根据用户id,在用户角色表中添加关联数据,事务处理
var result = 0; var userId = 0; using (var db = _Sql.Connection) using (var tran =db.BeginTransactio ...
- 磊哥的密码箱icpc11526
问题 D: 磊哥的密码箱 时间限制: 1 Sec 内存限制: 128 MB提交: 238 解决: 61[提交] [状态] [命题人:admin] 题目描述 磊哥有个密码箱,里面装的都是令磊哥羞羞的 ...
- ELK+Kafka
kafka:接收java程序投递的消息的日志队列 logstash:日志解析,格式化数据为json并输出到es中 elasticsearch:实时搜索搜索引擎,存储数据 kibana:基于es的数据可 ...
- 应用安全 - 中间件 - IIS - 漏洞 - 汇总
简介 支持协议 HTTP HTTP/2 HTTPS FTP FTPS SMTP NNTP等 支持操作系统 NT/2000/XP Professional/Server 2003及后续版本(XP Hom ...
- 利用BFS解决拯救007问题 -- 数据结构
题目: 7-1 拯救007 (30 分) 在老电影“007之生死关头”(Live and Let Die)中有一个情节,007被毒贩抓到一个鳄鱼池中心的小岛上,他用了一种极为大胆的方法逃脱 —— 直接 ...
- 2-django配置
一.settings.py配置 1.时区配置 现在看到的界面是英文的,将 LANGUAGE_CODE = 'en-us' 改为 LANGUAGE_CODE = 'zh-Hans '就可以看到如下界面 ...
- PythonWeb框架Django搭建过程
首先下载PyCharm专业版 破解地址:https://www.52pojie.cn/thread-997094-1-1.html 之后创建python虚拟环境(创建虚拟环境在上一篇博客) 激活虚拟环 ...
- ES分布式原理
参考:https://blog.csdn.net/chang384915878/article/details/86747419 一.准备知识 这里只是简单的介绍,详情可以看我的另一篇博客:https ...
- ThinkPHP验证器验证规则编码要点
首先验证器要继承框架的think\Validate类. 1.验证规则是一个父类的rule属性,是一个数组. 2.数组的键名是验证字段标识,值是验证规则.多个验证规则要用|分隔,不能有空格,否则可能会验 ...