Pandas选择数据
1、简单筛选
>>> dates = pd.date_range('', periods=6)
>>> df = pd.DataFrame(np.arange(24).reshape((6,4)),index=dates, columns=['A','B','C','D'])
>>> print(df)
A B C D
2013-01-01 0 1 2 3
2013-01-02 4 5 6 7
2013-01-03 8 9 10 11
2013-01-04 12 13 14 15
2013-01-05 16 17 18 19
2013-01-06 20 21 22 23 >>> print(df['A'])
2013-01-01 0
2013-01-02 4
2013-01-03 8
2013-01-04 12
2013-01-05 16
2013-01-06 20
Freq: D, Name: A, dtype: int32
>>> print(df.A)
2013-01-01 0
2013-01-02 4
2013-01-03 8
2013-01-04 12
2013-01-05 16
2013-01-06 20
Freq: D, Name: A, dtype: int32 #选择跨越多行或多列
>>> print(df[0:3])
A B C D
2013-01-01 0 1 2 3
2013-01-02 4 5 6 7
2013-01-03 8 9 10 11
>>> print(df['':''])
A B C D
2013-01-02 4 5 6 7
2013-01-03 8 9 10 11
2013-01-04 12 13 14 15
#如果df[3:3]将会是一个空对象。后者选择20130102到20130104标签之间的数据,并且包括这两个标签。
2、根据标签loc筛选
通过标签名字选择某一行数据, 或者通过选择某行或者所有行(:
代表所有行)然后选其中某一列或几列数据
>>> print(df.loc[''])
A 4
B 5
C 6
D 7
Name: 2013-01-02 00:00:00, dtype: int32 >>> print(df.loc[:,['A','B']])
A B
2013-01-01 0 1
2013-01-02 4 5
2013-01-03 8 9
2013-01-04 12 13
2013-01-05 16 17
2013-01-06 20 21 >>> print(df.loc['',['A','B']])
A 4
B 5
Name: 2013-01-02 00:00:00, dtype: int32
3、根据序列iloc
通过位置选择在不同情况下所需要的数据例如选某一个,连续选或者跨行选等操作。
>>> print(df.iloc[3,1])
13
>>> print(df.iloc[3:5,1:3])
B C
2013-01-04 13 14
2013-01-05 17 18
>>> print(df.iloc[[1,3,5],1:3])
B C
2013-01-02 5 6
2013-01-04 13 14
2013-01-06 21 22
4、混合loc、iloc两种的ix
>>> print(df.ix[:3,['A','C']])
A C
2013-01-01 0 2
2013-01-02 4 6
2013-01-03 8 10
5、通过判断的筛选
即可以采用判断指令 (Boolean indexing) 进行选择. 我们可以约束某项条件然后选择出当前所有数据.。
>>> print(df[df.A>8])
A B C D
2013-01-04 12 13 14 15
2013-01-05 16 17 18 19
2013-01-06 20 21 22 23
Pandas选择数据的更多相关文章
- pandas选择数据-【老鱼学pandas】
选择列 根据列名来选择某列的数据 import pandas as pd import numpy as np dates = pd.date_range("2017-01-08" ...
- Panda的学习之路(2)——pandas选择数据
首先定义panda dates=pd.date_range(',periods=6) # print(dates) df=pd.DataFrame(np.arange(24).reshape(6,4) ...
- 【转】Pandas学习笔记(二)选择数据
Pandas学习笔记系列: Pandas学习笔记(一)基本介绍 Pandas学习笔记(二)选择数据 Pandas学习笔记(三)修改&添加值 Pandas学习笔记(四)处理丢失值 Pandas学 ...
- pandas之数据选择
pandas中有三种索引方法:.loc,.iloc和[],注意:.ix的用法在0.20.0中已经不建议使用了 import pandas as pd import numpy as np In [5] ...
- pandas 学习 第14篇:索引和选择数据
数据框和序列结构中都有轴标签,轴标签的信息存储在Index对象中,轴标签的最重要的作用是: 唯一标识数据,用于定位数据 用于数据对齐 获取和设置数据集的子集. 本文重点关注如何对序列(Series)和 ...
- 【转载】使用Pandas对数据进行筛选和排序
使用Pandas对数据进行筛选和排序 本文转载自:蓝鲸的网站分析笔记 原文链接:使用Pandas对数据进行筛选和排序 目录: sort() 对单列数据进行排序 对多列数据进行排序 获取金额最小前10项 ...
- 【转载】使用Pandas创建数据透视表
使用Pandas创建数据透视表 本文转载自:蓝鲸的网站分析笔记 原文链接:使用Pandas创建数据透视表 目录 pandas.pivot_table() 创建简单的数据透视表 增加一个行维度(inde ...
- Python 数据分析 - 索引和选择数据
loc,iloc,ix三者间的区别和联系 loc .loc is primarily label based, but may also be used with a boolean array. 就 ...
- 基于pandas进行数据预处理
很久没用pandas,有些有点忘了,转载一个比较完整的利用pandas进行数据预处理的博文:https://blog.csdn.net/u014400239/article/details/70846 ...
随机推荐
- WPF 中关于 DataTemplateSelector 类的应用
MSDN的解释: 提供一种根据数据对象和与该数据绑定的元素来选择数据模板 DataTemplate 的方法. 示例代码: <Window x:Class="WpfApplication ...
- 吴裕雄 oracle 函数、触发器和包编程
- mysql 数据库必备命令操作,入门练习一下
mysql 数据库必备命令操作 show databases: 查看所有的数据库: create database jfedu: 创建名为jfedu数据库: use nihao: 进入jfedu数据库 ...
- Spring Data MongDB空间索引(判断一个点Point是否在一个区域Polygon内)
这里要连接MongoDB数据库,在配置文件里:spring.data.mongodb.uri = mongodb://root:root@localhost:27017/happy 两个root分别是 ...
- work单进程群发通知 后面会增加Channel组件的分组推送以及集群推送篇章
<?phpuse Workerman\Worker;use Workerman\Lib\Timer; require_once '../../web/Workerman/Autoloader.p ...
- php json中文被转义
php 5.4 json_encode($str, JSON_UNESCAPED_UNICODE); 5.4版本以下 方法一function encode_json($str){ $code = js ...
- java aop的理解
https://www.cnblogs.com/mafly/p/SpringAOP.html
- 设计table表格,用js设计偶数行和奇数行显示不同的颜色
第一种:鼠标经过时table表格中的颜色根据奇偶行改变不同的颜色 <!DOCTYPE html> <html> <head> <meta charset=&q ...
- phpunit 生成三种日志文件的配置方法
#目录结构 windows bin目录下 ├── phpunit.phar ├── phpunit.cmd ├── phpunit.xml ├── build.xml ├── ArrTest.php ...
- JPA in
CriteriaBuilder.In in = criteriaBuilder.in(root.get("field1")); for (String str : arr) { i ...