Series索引的工作方式类似于NumPy数组的索引,不过Series的索引值不只是整数,如:

import numpy as np
import pandas as pd
from pandas import Series,DataFrame
obj=Series(np.arange(4),index=['a','b','c','d'])
obj=Series(np.arange(4),index=['a','b','c','d'])
obj
Out[10]:
a 0
b 1
c 2
d 3
dtype: int32
obj['b']
Out[11]: 1 obj[1]
Out[12]: 1 obj[2:4]
Out[13]:
c 2
d 3
dtype: int32 obj[['b','a','d']]
Out[14]:
b 1
a 0
d 3
dtype: int32 obj[[1,3]]
Out[15]:
b 1
d 3
dtype: int32

obj[obj<2]
Out[17]:
a 0
b 1
dtype: int32

#利用标签索引与普通的Python切片运算不同
#因为末端是包含的
obj['b':'c']=5 obj
Out[24]:
a 0
b 5
c 5
d 3
dtype: int32
 
DataFrame 进行索引其实就是获取一个或者多个列:
 
获取列:指定列名称即可
data=DataFrame(np.arange(16).reshape((4,4)),index=['Ohio','Colorado','Utah','New York'],columns=['one','two','three','four'])

data
Out[26]:
one two three four
Ohio 0 1 2 3
Colorado 4 5 6 7
Utah 8 9 10 11
New York 12 13 14 15 data['two']
Out[27]:
Ohio 1
Colorado 5
Utah 9
New York 13
Name: two, dtype: int32 data[['three','one']]
Out[28]:
three one
Ohio 2 0
Colorado 6 4
Utah 10 8
New York 14 12
获取行:
(1)通过切片或布尔型数组;
(2)通过布尔型DataFrame进行索引;
(3)在行上标签索引,引入索引字段ix,它可以通过NumPy式的标记法及轴标签从DataFrame中选取行和列的子集。

#切片获取行
data[:2]
Out[29]:
one two three four
Ohio 0 1 2 3
Colorado 4 5 6 7 #布尔型数组获取行
data[data['three']>5]
Out[30]:
one two three four
Colorado 4 5 6 7
Utah 8 9 10 11
New York 12 13 14 15 #布尔型DataFrame进行索引
data<5
Out[31]:
one two three four
Ohio True True True True
Colorado True False False False
Utah False False False False
New York False False False False #将data<5的数值赋值为0
data[data<5]=0 data
Out[33]:
one two three four
Ohio 0 0 0 0
Colorado 0 5 6 7
Utah 8 9 10 11
New York 12 13 14 15 #行上进行标签索引,使用索引字段ix
data.ix['Colorado',['two','three']]
Out[34]:
two 5
three 6
Name: Colorado, dtype: int32 data.ix[['Colorado','Utah'],[3,0,1]]
Out[35]:
four one two
Colorado 7 0 5
Utah 11 8 9 #索引的是行索引号为2的数据,也就是行Utah
data.ix[2]
Out[36]:
one 8
two 9
three 10
four 11
Name: Utah, dtype: int32 data.ix[:'Utah','two']
Out[37]:
Ohio 0
Colorado 5
Utah 9
Name: two, dtype: int32 #索引data.three>5的行
data.ix[data.three>5,:3]
Out[38]:
one two three
Colorado 0 5 6
Utah 8 9 10
New York 12 13 14

DataFrame的索引选项

#选取DataFrame的单个列或者一组列
obj[val]
#选取的单个行或者一组行
obj.ix[val]
#选取单个列或列的子集
obj.ix[:,val]
#同时选取行和列
obj.ix[val1,val2]
 

pandas 索引、选取和过滤的更多相关文章

  1. Pandas DataFrame 数据选取和过滤

    This would allow chaining operations like: pd.read_csv('imdb.txt') .sort(columns='year') .filter(lam ...

  2. python数据分析之pandas数据选取:df[] df.loc[] df.iloc[] df.ix[] df.at[] df.iat[]

    1 引言 Pandas是作为Python数据分析著名的工具包,提供了多种数据选取的方法,方便实用.本文主要介绍Pandas的几种数据选取的方法. Pandas中,数据主要保存为Dataframe和Se ...

  3. pandas子集选取的三种方法:[]、.loc[]、.iloc[]

    pandas读取Excel.csv文件中的数据时,得到的大多是表格型的二维数据,在pandas中对应的即为DataFrame数据结构.在处理这类数据时,往往要根据据需求先获取数据中的子集,如某些列.某 ...

  4. 【python】pandas 索引操作

    选择.修改数据(单层索引) 推荐使用.at..iat..loc..iloc 操作 句法 结果 备注 选择列 df[col] Series 基于列名(列的标签),返回Series 用标签选择行 df.l ...

  5. python pandas.DataFrame选取、修改数据最好用.loc,.iloc,.ix

    先手工生出一个数据框吧 import numpy as np import pandas as pd df = pd.DataFrame(np.arange(0,60,2).reshape(10,3) ...

  6. pandas 索引与列相互转化

    1. 准备数据 import pandas as pd from io import StringIO csv_txt = '''"date","player1" ...

  7. Pandas索引和选择数据

    在本章中,我们将讨论如何切割和丢弃日期,并获取Pandas中大对象的子集. Python和NumPy索引运算符"[]"和属性运算符".". 可以在广泛的用例中快 ...

  8. pandas索引操作

    Pandas的索引操作 索引对象Index 1. Series和DataFrame中的索引都是Index对象 示例代码: print(type(ser_obj.index)) print(type(d ...

  9. Pandas 索引和切片

    Series和Datafram索引的原理一样,我们以Dataframe的索引为主来学习 列索引:df['列名'] (Series不存在列索引) 行索引:df.loc[].df.iloc[] 选择列 / ...

随机推荐

  1. XStream的简单使用

    XStream XStream是一个java对象和xml相互转换的工具 创建XStream对象:XStream stream = new XStream() Java对象转换成xml:stream . ...

  2. 搜索solr

    这是我第一次写博客,没有系统性.专业性,东西很杂,也不知道自己在写些什么. SOA分布式架构,所以,使用solr,搜索层的服务层需要搭建起来.搜索系统的表现层搭建 ,打包方式是war包 域名改变代表系 ...

  3. nginx proxy_pass设置

    NGINX服务器的反向代理PROXY_PASS配置方法讲解 https://www.cnblogs.com/lianxuan1768/p/8383804.html Nginx配置proxy_pass转 ...

  4. 【LeetCode】Stack

    [503] Next Greater Element II [Medium] 给一个循环数组,找到离当前元素最近的比它大的元素. Input: [1,2,1] Output: [2,-1,2] Exp ...

  5. mysql莫名报"unknown column ... in 'on clause'"

    今天遇见个会诡异的问题 一个web程序本地调试的好好的,结果发布到服务器上程序就报错了,报"unknown column ... in 'on clause'",网上搜了下,说是m ...

  6. git - Mac生成SSH key

    步骤1.检查是否已经存在SSH Key 打开电脑终端,输入以下命令: ls -al ~/.ssh 会出现两种情况 步骤2. 生成/设置SSH Key 继续上一步可能出现的情况 (1)情况一: 终端出现 ...

  7. 【LeetCode 14】最长公共前缀

    题目链接 [题解] 二分最长前缀的长度. 然后暴力把第2..n个字符串和第1个字符串的前mid个字符匹配. 还有一种比较厉害的算法. 把这n个字符串加入到字典树当中去. 然后根节点到第一个有分支的节点 ...

  8. Android中ViewPgae中的Fragment如何确认当前页面可见的问题

    由于在ViewPage中PageAdapter来管理所有的Fragment.在加载一个Fragment的时候,会自动缓存左右几个(默认是一个)页面,此时也会调用到正常的生命周期函数,onCreate, ...

  9. stl+数论——1247D

    其实也不算很难想,每个元素质因子分解后的p^c的p和c用pair的形式存在每个元素vector里 要去前面找一个数使得所有指数相加是k的倍数,那么把vector里的所有c 模 k,然后去找前面互补的数 ...

  10. Shell基础(二):Shell中的数值运算、条件测试操作、使用if选择结构

    一.Shell中的数值运算 目标: 本案例要求熟悉Linux Shell环境的特点,主要练习以下操作: 1> 使用expr.$[ ].let等整数运算工具:定义变量X=1234,然后计算X与78 ...