【Python】pandas 筛选数据的 8 个骚操作
肝了一个月的pandas进阶小册子出炉了👉「pandas进阶宝典」
日常用Python
做数据分析最常用到的就是查询筛选了,按各种条件、各种维度以及组合挑出我们想要的数据,以方便我们分析挖掘。
东哥总结了日常查询和筛选常用的种骚操作,供各位学习参考。本文采用sklearn
的boston
数据举例介绍。
from sklearn import datasets
import pandas as pd
boston = datasets.load_boston()
df = pd.DataFrame(boston.data, columns=boston.feature_names)

1. []
第一种是最快捷方便的,直接在dataframe的[]
中写筛选的条件或者组合条件。比如下面,想要筛选出大于NOX
这变量平均值的所有数据,然后按NOX
降序排序。
df[df['NOX']>df['NOX'].mean()].sort_values(by='NOX',ascending=False).head()

当然,也可以使用组合条件,条件之间使用逻辑符号& |
等。比如下面这个例子除了上面条件外再加上且条件CHAS为1
,注意逻辑符号分开的条件要用()
隔开。
df[(df['NOX']>df['NOX'].mean())& (df['CHAS'] ==1)].sort_values(by='NOX',ascending=False).head()

2. loc/iloc
除[]
之外,loc/iloc
应该是最常用的两种查询方法了。loc
按标签值(列名和行索引取值)访问,iloc
按数字索引访问,均支持单值访问或切片查询。除了可以像[]
按条件筛选数据以外,loc
还可以指定返回的列变量,从行和列两个维度筛选。
比如下面这个例子,按条件筛选出数据,并筛选出指定变量,然后赋值。
df.loc[(df['NOX']>df['NOX'].mean()),['CHAS']] = 2

3. isin
上面我们筛选条件< > == !=
都是个范围,但很多时候是需要锁定某些具体的值的,这时候就需要isin
了。比如我们要限定NOX
取值只能为0.538,0.713,0.437
中时。
df.loc[df['NOX'].isin([0.538,0.713,0.437]),:].sample(5)

当然,也可以做取反操作,在筛选条件前加~
符号即可。
df.loc[~df['NOX'].isin([0.538,0.713,0.437]),:].sample(5)

4. str.contains
上面的举例都是数值大小比较的筛选条件,除数值以外当然也有字符串的查询需求。pandas
里实现字符串的模糊筛选,可以用.str.contains()
来实现,有点像在SQL语句里用的是like
。
下面利用titanic的数据举例,筛选出人名中包含Mrs
或者Lily
的数据,|
或逻辑符号在引号内。
train.loc[train['Name'].str.contains('Mrs|Lily'),:].head()

.str.contains()
中还可以设置正则化筛选逻辑。
case=True:使用case指定区分大小写
na=True:就表示把有NAN的转换为布尔值True
flags=re.IGNORECASE:标志传递到re模块,例如re.IGNORECASE
regex=True:regex :如果为True,则假定第一个字符串是正则表达式,否则还是字符串
5. where/mask
在SQL里,我们知道where
的功能是要把满足条件的筛选出来。pandas中where
也是筛选,但用法稍有不同。
where
接受的条件需要是布尔类型的,如果不满足匹配条件,就被赋值为默认的NaN
或其他指定值。举例如下,将Sex
为male
当作筛选条件,cond
就是一列布尔型的Series,非male的值就都被赋值为默认的NaN
空值了。
cond = train['Sex'] == 'male'
train['Sex'].where(cond, inplace=True)
train.head()

也可以用other
赋给指定值。
cond = train['Sex'] == 'male'
train['Sex'].where(cond, other='FEMALE', inplace=True)

甚至还可以写组合条件。
train['quality'] = ''
traincond1 = train['Sex'] == 'male'
cond2 = train['Age'] > 25
train['quality'].where(cond1 & cond2, other='低质量男性', inplace=True)

mask
和where
是一对操作,与where
正好反过来。
train['quality'].mask(cond1 & cond2, other='低质量男性', inplace=True)

6. query
这是一种非常优雅的筛选数据方式。所有的筛选操作都在''
之内完成。
# 常用方式
train[train.Age > 25]
# query方式
train.query('Age > 25')
上面的两种方式效果上是一样的。再比如复杂点的,加入上面的str.contains
用法的组合条件,注意条件里有''
时,两边要用""
包住。
train.query("Name.str.contains('William') & Age > 25")

在query
里还可以通过@
来设定变量。
name = 'William'
train.query("Name.str.contains(@name)")
7. filter
filter
是另外一个独特的筛选功能。filter
不筛选具体数据,而是筛选特定的行或列。它支持三种筛选方式:
items:固定列名
regex:正则表达式
like:以及模糊查询
axis:控制是行index或列columns的查询
下面举例介绍下。
train.filter(items=['Age', 'Sex'])

train.filter(regex='S', axis=1) # 列名包含S的

train.filter(like='2', axis=0) # 索引中有2的

train.filter(regex='^2', axis=0).filter(like='S', axis=1)

8. any/all
any
方法意思是,如果至少有一个值为True
结果便为True
,all
需要所有值为True
结果才为True
,比如下面这样。
>> train['Cabin'].all()
>> False
>> train['Cabin'].any()
>> True
any
和all
一般是需要和其它操作配合使用的,比如查看每列的空值情况。
train.isnull().any(axis=0)

再比如查看含有空值的行数。
>>> train.isnull().any(axis=1).sum()
>>> 708
参考:
[1] https://pandas.pydata.org/
[2] https://www.gairuo.com/p/pandas-selecting-data
[3] https://blog.csdn.net/weixin_43484764/article/details/89847241
往期精彩回顾
适合初学者入门人工智能的路线及资料下载(图文+视频)机器学习入门系列下载机器学习及深度学习笔记等资料打印《统计学习方法》的代码复现专辑机器学习交流qq群955171419,加入微信群请扫码