This would allow chaining operations like:

pd.read_csv('imdb.txt')
.sort(columns='year')
.filter(lambda x: x['year']>1990) # <---this is missing in Pandas
.to_csv('filtered.csv')

For current alternatives see:

http://stackoverflow.com/questions/11869910/pandas-filter-rows-of-dataframe-with-operator-chaining

可以这样:

df = pd.read_csv('imdb.txt').sort(columns='year')
df[df['year']>1990].to_csv('filtered.csv')

  

# however, could potentially do something like this:

pd.read_csv('imdb.txt')
.sort(columns='year')
.[lambda x: x['year']>1990]
.to_csv('filtered.csv')
or pd.read_csv('imdb.txt')
.sort(columns='year')
.loc[lambda x: x['year']>1990]
.to_csv('filtered.csv')

  

from:https://yangjin795.github.io/pandas_df_selection.html

Pandas 是 Python Data Analysis Library, 是基于 numpy 库的一个为了数据分析而设计的一个 Python 库。它提供了很多工具和方法,使得使用 python 操作大量的数据变得高效而方便。

本文专门介绍 Pandas 中对 DataFrame 的一些对数据进行过滤、选取的方法和工具。 首先,本文所用的原始数据如下:

df = pd.DataFrame(np.random.randn(6,4), index=dates, columns=list('ABCD'))
    Out[9]:
A B C D
2017-04-01 0.522241 0.495106 -0.268194 -0.035003
2017-04-02 2.104572 -0.977768 -0.139632 -0.735926
2017-04-03 0.480507 1.215048 1.313314 -0.072320
2017-04-04 1.700309 0.287588 -0.012103 0.525291
2017-04-05 0.526615 -0.417645 0.405853 -0.835213
2017-04-06 1.143858 -0.326720 1.425379 0.531037

选取

通过 [] 来选取

选取一列或者几列:

df['A']
Out:
2017-04-01 0.522241
2017-04-02 2.104572
2017-04-03 0.480507
2017-04-04 1.700309
2017-04-05 0.526615
2017-04-06 1.143858
df[['A','B']]
Out:
A B
2017-04-01 0.522241 0.495106
2017-04-02 2.104572 -0.977768
2017-04-03 0.480507 1.215048
2017-04-04 1.700309 0.287588
2017-04-05 0.526615 -0.417645
2017-04-06 1.143858 -0.326720

选取某一行或者几行:

df['2017-04-01':'2017-04-01']
Out:
A B C D
2017-04-01 0.522241 0.495106 -0.268194 -0.03500
df['2017-04-01':'2017-04-03']
A B C D
2017-04-01 0.522241 0.495106 -0.268194 -0.035003
2017-04-02 2.104572 -0.977768 -0.139632 -0.735926
2017-04-03 0.480507 1.215048 1.313314 -0.072320

loc, 通过行标签选取数据

df.loc['2017-04-01','A']
df.loc['2017-04-01']
Out:
A 0.522241
B 0.495106
C -0.268194
D -0.035003
df.loc['2017-04-01':'2017-04-03']
Out:
A B C D
2017-04-01 0.522241 0.495106 -0.268194 -0.035003
2017-04-02 2.104572 -0.977768 -0.139632 -0.735926
2017-04-03 0.480507 1.215048 1.313314 -0.072320
df.loc['2017-04-01':'2017-04-04',['A','B']]
Out:
A B
2017-04-01 0.522241 0.495106
2017-04-02 2.104572 -0.977768
2017-04-03 0.480507 1.215048
2017-04-04 1.700309 0.287588
df.loc[:,['A','B']]
Out:
A B
2017-04-01 0.522241 0.495106
2017-04-02 2.104572 -0.977768
2017-04-03 0.480507 1.215048
2017-04-04 1.700309 0.287588
2017-04-05 0.526615 -0.417645
2017-04-06 1.143858 -0.326720

iloc, 通过行号获取数据

df.iloc[2]
Out:
A 0.480507
B 1.215048
C 1.313314
D -0.072320
df.iloc[1:3]
Out:
A B C D
2017-04-02 2.104572 -0.977768 -0.139632 -0.735926
2017-04-03 0.480507 1.215048 1.313314 -0.072320
df.iloc[1,1]

df.iloc[1:3,1]

df.iloc[1:3,1:2]

df.iloc[[1,3],[2,3]]
Out:
C D
2017-04-02 -0.139632 -0.735926
2017-04-04 -0.012103 0.525291 df.iloc[[1,3],:] df.iloc[:,[2,3]]

iat, 获取某一个 cell 的值

df.iat[1,2]
Out:
-0.13963224781812655

过滤

使用 [] 过滤

[]中是一个boolean 表达式,凡是计算为 True 的就会被选取。

df[df.A>1]
Out:
A B C D
2017-04-02 2.104572 -0.977768 -0.139632 -0.735926
2017-04-04 1.700309 0.287588 -0.012103 0.525291
2017-04-06 1.143858 -0.326720 1.425379 0.531037
df[df>1]
Out:
A B C D
2017-04-01 NaN NaN NaN NaN
2017-04-02 2.104572 NaN NaN NaN
2017-04-03 NaN 1.215048 1.313314 NaN
2017-04-04 1.700309 NaN NaN NaN
2017-04-05 NaN NaN NaN NaN
2017-04-06 1.143858 NaN 1.425379 NaN df[df.A+df.B>1.5]
Out:
A B C D
2017-04-03 0.480507 1.215048 1.313314 -0.072320
2017-04-04 1.700309 0.287588 -0.012103 0.525291

下面是一个更加复杂的例子,选取的是 index 在 '2017-04-01'中'2017-04-04'的,一行的数据的和大于1的行:

df.loc['2017-04-01':'2017-04-04',df.sum()>1]

还可以通过和 apply 方法结合,构造更加复杂的过滤,实现将某个返回值为 boolean 的方法作为过滤条件:

df[df.apply(lambda x: x['b'] > x['c'], axis=1)]

使用 isin

df['E']=['one', 'one','two','three','four','three']
A B C D E
2017-04-01 0.522241 0.495106 -0.268194 -0.035003 one
2017-04-02 2.104572 -0.977768 -0.139632 -0.735926 one
2017-04-03 0.480507 1.215048 1.313314 -0.072320 two
2017-04-04 1.700309 0.287588 -0.012103 0.525291 three
2017-04-05 0.526615 -0.417645 0.405853 -0.835213 four
2017-04-06 1.143858 -0.326720 1.425379 0.531037 three df[df.E.isin(['one'])]
Out:
A B C D E
2017-04-01 0.522241 0.495106 -0.268194 -0.035003 one
2017-04-02 2.104572 -0.977768 -0.139632 -0.735926 one

Pandas DataFrame 数据选取和过滤的更多相关文章

  1. Pandas DataFrame数据的增、删、改、查

    Pandas DataFrame数据的增.删.改.查 https://blog.csdn.net/zhangchuang601/article/details/79583551 #删除列 df_2 = ...

  2. pandas 索引、选取和过滤

    Series索引的工作方式类似于NumPy数组的索引,不过Series的索引值不只是整数,如: import numpy as np import pandas as pd from pandas i ...

  3. Pandas dataframe数据写入文件和数据库

    转自:http://www.dcharm.com/?p=584 Pandas是Python下一个开源数据分析的库,它提供的数据结构DataFrame极大的简化了数据分析过程中一些繁琐操作,DataFr ...

  4. Pandas:DataFrame数据选择方法(索引)

    #首先创建我们的Series对象,然后合并到dataframe对象里面去 import pandas as pd import numpy as np area=pd.Series({,,,}) po ...

  5. pandas DataFrame数据转为list

    dfpath=df[df['mm'].str.contains('20180122\d')].values dfplist=np.array(dfpath).tolist()

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

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

  7. Python3 Pandas的DataFrame数据的增、删、改、查

    Python3 Pandas的DataFrame数据的增.删.改.查 一.DataFrame数据准备 增.删.改.查的方法有很多很多种,这里只展示出常用的几种. 参数inplace默认为False,只 ...

  8. pandas.DataFrame——pd数据框的简单认识、存csv文件

    接着前天的豆瓣书单信息爬取,这一篇文章看一下利用pandas完成对数据的存储. 回想一下我们当时在最后得到了六个列表:img_urls, titles, ratings, authors, detai ...

  9. pandas dataframe重复数据查看.判断.去重

    本文详解如何使用pandas查看dataframe的重复数据,判断是否重复,以及如何去重 dataframe数据样本: import pandas as pd df = pd.DataFrame({' ...

随机推荐

  1. P4878 [USACO05DEC]layout布局

    P4878 [USACO05DEC]layout布局 差分约束 最短路径最长路,最长路径最短路 本题求的是最长路径,所以跑最短路 根据题意连边,然后spfa即可 注意要判断图的连通性,所以新建一个虚拟 ...

  2. (GO_GTD_3)基于OpenCV和QT,建立Android图像处理程序

    一.解决权限问题     图片采集了,处理了,如何保存?最直接的方法是使用imwrite,但是如果现在直接使用的话,比如会出现这样或那样的错误,因为我们现在是在android的环境下进行图像处理,所以 ...

  3. STM32.BOOT

    BOOT0 和 BOOT1STM32 三种启动模式对应的存储介质均是芯片内置的,它们是:1)用户闪存 = 芯片内置的?Flash.2)SRAM = 芯片内置的 RAM 区,就是内存啦.3)系统存储器 ...

  4. node包管理工具--nvm(windows)

    windows 安装nvw-windows 使用nvm工具: windows使用nvm-noinstall.zip安装 nvm-noinstall.zip 这个是绿色免安装版本,但是使用之前需要配置 ...

  5. div转svg svg转canvas svg生成图片及图片下载 分享

    链接来自:http://blog.csdn.net/u010081689/article/details/50728854

  6. luoguP2572 [SCOI2010]序列操作

    题目&&链接 反正数据都是一样的,luogu比较友好 luogu bzoj lxhgww最近收到了一个01序列,序列里面包含了n个数,这些数要么是0,要么是1,现在对于这个序列有五种变 ...

  7. 如何在windows中部署Gitblit

    1.安装Java环境 2.下载Gitblit压缩包 http://gitblit.com/ 3.解压后进行配置 编辑gitblit-1.8.0\data\gitblit.properties文件 gi ...

  8. System.ConfigurationManager类用于对配置文件的读取

    http://blog.csdn.net/ligenyingsr/article/details/54095986 System.ConfigurationManager类用于对配置文件的读取.其具有 ...

  9. yarn虚拟cpu和虚拟内存

    虚拟cpu 虚拟的cpu代码并发数,如果一个container拥有2个vcpu,那么该container就可以真正的在同一时间运行两个线程,而不是靠切时间片而达到的逻辑并发.所以一般虚拟的cpu需要和 ...

  10. HDU 6143 Killer Names(容斥原理)

    http://acm.hdu.edu.cn/showproblem.php?pid=6143 题意: 用m个字母去取名字,名字分为前后两部分,各由n个字符组成,前后两部分不能出现相同字符,问合法的组成 ...