pandas取值
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/5/24 15:03
# @Author : zhang chao
# @File : s.py
from scipy import linalg as lg
#按标签选择
#通过标签选择多轴 import pandas as pd
import numpy as np dates = pd.date_range('', periods=6)
df = pd.DataFrame(np.random.randn(6,4), index=dates, columns=list('ABCD'))
print("df:")
print(df)
print('-'*50)
#通过索引选择
print("df.loc[:,['A','B']]")
print(df.loc[:,['A','B']])
#显示标签切片,包括两个端点
print('-'*50)
print("df.loc['20170102':'20170104',['A','B']]")
print(df.loc['':'',['A','B']])
print('-'*50)
#获得标量值 获取m行n列的单个数据值
print("df.loc[dates[0],'A'")
print(df.loc[dates[0],'A'])
#快速访问标量(等同于先前的方法)
print('-'*50)
print("df.at[dates[0],'A']")
print(df.at[dates[0],'A'])#at快速访问单个值;loc访问多指
#通过传递的整数的位置选择 通过下标选择
print("df.iloc[3]")
print(df.iloc[3])
print('-'*50)
#通过整数切片,类似于numpy/python
print("df.iloc[3:5,0:2]")
print(df.iloc[3:5,0:2])#切片
#通过整数位置的列表,类似于numpy/python样式
print("df.iloc[[1,2,4],[0,2]]")
print(df.iloc[[1,2,4],[0,2]])#列表
print("df.iloc[1:3,:]")
print(df.iloc[1:3,:])
print("df.iloc[:,1:3]")
print(df.iloc[:,1:3])
print("df.iloc[1,1]")
print(df.iloc[1,1])
#要快速访问标量(等同于先前的方法)
print("print(df.iat[1,1])")
print(df.iat[1,1])
#布尔索引
#使用单列的值来选择数据
print("df[df.A > 0]")
print(df[df.A > 0][df.B<0])#多条件选择
print("df[df > 0]")
print(df[df > 0])#从满足布尔条件的DataFrame中选择值
#使用isin()方法进行过滤
df2 = df.copy()
df2['E'] = ['one', 'one','two','three','four','three']
print("df2")
print(df2)
print("============= start to filter =============== ")
print("isin")
print(df2[df2['E'].isin(['two','four'])])
D:\Download\python3\python3.exe D:/Download/pycharmworkspace/s.py
df:
A B C D
2017-01-01 -1.353900 -0.737163 -0.266858 -0.219116
2017-01-02 -2.328935 0.297892 0.244013 0.331435
2017-01-03 0.442864 -1.837813 -0.523082 -1.058623
2017-01-04 -2.117530 -0.480186 0.174002 -0.197551
2017-01-05 -0.312444 -0.958863 0.004229 -0.998425
2017-01-06 0.957020 -0.147027 0.125730 -0.643826
--------------------------------------------------
df.loc[:,['A','B']] #loc为原始索引 用键索引 字符索引
A B
2017-01-01 -1.353900 -0.737163
2017-01-02 -2.328935 0.297892
2017-01-03 0.442864 -1.837813
2017-01-04 -2.117530 -0.480186
2017-01-05 -0.312444 -0.958863
2017-01-06 0.957020 -0.147027
--------------------------------------------------
df.loc['20170102':'20170104',['A','B']]
A B
2017-01-02 -2.328935 0.297892
2017-01-03 0.442864 -1.837813
2017-01-04 -2.117530 -0.480186
--------------------------------------------------
df.loc[dates[0],'A'
-1.3539004392106717
--------------------------------------------------
df.at[dates[0],'A']#at快速取值
-1.3539004392106717
--------------------------------------------------
df.iloc[3]#iloc为数字索引
A -2.117530
B -0.480186
C 0.174002
D -0.197551
Name: 2017-01-04 00:00:00, dtype: float64
--------------------------------------------------
df.iloc[3:5,0:2]
A B
2017-01-04 -2.117530 -0.480186
2017-01-05 -0.312444 -0.958863
--------------------------------------------------
df.iloc[[1,2,4],[0,2]]
A C
2017-01-02 -2.328935 0.244013
2017-01-03 0.442864 -0.523082
2017-01-05 -0.312444 0.004229
--------------------------------------------------
df.iloc[1:3,:]
A B C D
2017-01-02 -2.328935 0.297892 0.244013 0.331435
2017-01-03 0.442864 -1.837813 -0.523082 -1.058623
--------------------------------------------------
df.iloc[:,1:3]
B C
2017-01-01 -0.737163 -0.266858
2017-01-02 0.297892 0.244013
2017-01-03 -1.837813 -0.523082
2017-01-04 -0.480186 0.174002
2017-01-05 -0.958863 0.004229
2017-01-06 -0.147027 0.125730
--------------------------------------------------
df.iloc[1,1]
0.29789175201181145
--------------------------------------------------
print(df.iat[1,1])#iat快速数字索引取值
0.29789175201181145
--------------------------------------------------
df[df.A > 0]#按照A列的元素大于0 进行筛选取值
A B C D
2017-01-03 0.442864 -1.837813 -0.523082 -1.058623
2017-01-06 0.957020 -0.147027 0.125730 -0.643826
--------------------------------------------------
df[df > 0]#保留数据大于0的元素,费大于0的元素为NaN
A B C D
2017-01-01 NaN NaN NaN NaN
2017-01-02 NaN 0.297892 0.244013 0.331435
2017-01-03 0.442864 NaN NaN NaN
2017-01-04 NaN NaN 0.174002 NaN
2017-01-05 NaN NaN 0.004229 NaN
2017-01-06 0.957020 NaN 0.125730 NaN
--------------------------------------------------
df2
A B C D E
2017-01-01 -1.353900 -0.737163 -0.266858 -0.219116 one
2017-01-02 -2.328935 0.297892 0.244013 0.331435 one
2017-01-03 0.442864 -1.837813 -0.523082 -1.058623 two
2017-01-04 -2.117530 -0.480186 0.174002 -0.197551 three
2017-01-05 -0.312444 -0.958863 0.004229 -0.998425 four
2017-01-06 0.957020 -0.147027 0.125730 -0.643826 three
--------------------------------------------------
============= start to filter ===============
isin
df2['E'].isin(['two','four'])
df2[df2['E'].isin(['two','four'])]
#如果E列中的元素在 isin里面 则获取到值
A B C D E
2017-01-03 0.442864 -1.837813 -0.523082 -1.058623 two
2017-01-05 -0.312444 -0.958863 0.004229 -0.998425 four
Process finished with exit code 0
pandas取值的更多相关文章
- Python数据科学手册-Pandas:数据取值与选择
Numpy数组取值 切片[:,1:5], 掩码操作arr[arr>0], 花哨的索引 arr[0, [1,5]],Pandas的操作类似 Series数据选择方法 Series对象与一维Nump ...
- 告别硬编码,mysql 如何实现按某字段的不同取值进行统计
上周我突然意识到,我在grafana上写的 sql 语句存在多处硬编码.这篇笔记将记录如何实现没有硬编码的sql语句,以及自学编程过程中如何应对自己的笨拙代码和难题不断的状况. 1.有效但粗笨的硬编码 ...
- 如何解决流程开发中SheetRadioButtonList页面取值问题
分享一个常见的取值问题. 应用场景: SheetRadioButtonList控件,点击其中一项执行事件操作.如果是页面加载的情况下,值就无法取到. 具体原因如下: 我给SheetRadioButto ...
- jQuery radio的取值与赋值
取值: $("input[name='radioName']:checked").val(); 赋值: $("input[name='radioName'][value= ...
- python通过函数改变变量取值
严格讲应该是"通过函数调用,改变引用对象".python中,要区分"变量名"和"对象" 如果是类的对象,是引用类型的,那么可以通过函数调用, ...
- jsf初学selectOneMenu 绑定与取值
jsf 的selectOneMenu 最后生成的<select>标签.这里涉及到一个binding 起初一直不知道是干嘛的,后来参考了其他文章.就相当于在asp.net 中如:<as ...
- CYQ.Data 快速开发之UI(赋值、取值、绑定)原理
昨夜园子猴子问了几个我CYQ.Data使用的小问题,经过简单解答后,他表示“妈妈再也不用担心我的学习",并于事后以资鼓励,希望这框架越走越好. 除了技术上的交流,双方在生活,S上面的问题上也 ...
- EditText的inputType常用取值
最近经过实际试验,总结了InputType几个常用取值表示的含义: 1.none, text, textVisiblePassword: 无任何输入限制 2.textMultiLine: 允许多行输入 ...
- jquery select取值,赋值操作
select">jquery select取值,赋值操作 一.获取Select 获取select 选中的 text : $("#ddlRegType").find( ...
随机推荐
- go标准库的学习-crypto/sha1
参考:https://studygolang.com/pkgdoc 导入方式: import "crypto/sha1" sha1包实现了SHA1哈希算法,参见RFC 3174. ...
- Database hang and Row Cache Lock concurrency troubleshooting
http://www.dadbm.com/database-hang-row-cache-lock-concurrency-troubleshooting/ Issue backgroundThis ...
- 类似Visual Studio一样,使用Qt Creator管理多个项目,创建子项目
1. 简介 QtCreator是一个十分好用的跨平台IDE,由于最近需要在Windows和Mac同时写一个C++的代码,使用VS和XCode不能实现项目的统一管理(可以使用cmake来组织源码,但是每 ...
- Python虚拟环境和包管理工具Pipenv的使用详解--看完这一篇就够了
前言 Python虚拟环境是一个虚拟化,从电脑独立开辟出来的环境.在这个虚拟环境中,我们可以pip安装各个项目不同的依赖包,从全局中隔离出来,利于管理. 传统的Python虚拟环境有virtualen ...
- MySQL 基础六 临时表 复制表结构
1.临时表CREATE TEMPORARY TABLE test2( id INT ) SELECT *FROM test2 SHOW TABLES; INSERT INTO test2 VALUES ...
- ubuntu 14.04 安装jdk 1.8
一,如何删除低版本的open JDK? 在ubuntn的软件中心中,如果输入"Java",我们会看到open JDK,但是最高版本是1.7,也有1.6版本的,如果我们安装上去,可能 ...
- Apache IOUtils的使用
IOUtils 与 FileUtilsCommons IO 是 apache 的一个开源的工具包,封装了 IO操作的相关类,使用 Commons IO 可以很方便的读写文件 commons.jar 包 ...
- IIS 8的第一次请求不变慢如何配置
首先需要在Window中添加Application Initialization 在IIS中配置Application Pool 在IIS配置Web Site 配置完成,如果版本在7.5,可以下载:A ...
- PHPStorm FTP upload could not change to work directory 无法更改目录
使用PHPStorm 2016 2.2版本 设置代码及时上传的时候遇到了这个问题,无法上传代码. 配置好了FTP之后去测试,是正常的,如下图一所示,也开启了那个被动模式(见图二),但是去上传代码的时候 ...
- 方差(variance)、标准差(Standard Deviation)、均方差、均方根值(RMS)、均方误差(MSE)、均方根误差(RMSE)
方差(variance).标准差(Standard Deviation).均方差.均方根值(RMS).均方误差(MSE).均方根误差(RMSE) 2017年10月08日 11:18:54 cqfdcw ...