PYTHON数据预处理_DATAFRAME数据筛选LOC,ILOC,IX,AT,IAT


众所周知 pandasDataFrame数据结构提供了功能强大的数据操作功能,例如运算,筛选,统计等。
今天我们就来谈一谈其强大的数据筛选功能,主要包括两大类, 按照条件筛选按照索引筛选。可以 对行进行筛选,也可以 按照列进行筛选

import numpy as np
import pandas as pd

df = pd.DataFrame({“a”: list(range(1,10,1)),“b”: list(range(2,11,1)),“c”: list(range(3,12,1))})

  • 1
  • 2
  • 3
  • 4
  • 5
df

 
 
  • 1
abc
0123
1234
2345
3456
4567
5678
6789
78910
891011

1.条件筛选

1.1 单条件筛选
  • 如果选取a列的取值大于3的记录可以这么写
df[df['a']>3]

 
 
  • 1
abc
3456
4567
5678
6789
78910
891011
  • 如果想筛选a列的取值大于3的记录,但是只显示满足条件的b,c列的值可以这么写
df[['b','c']][df['a']>3]

 
 
  • 1
bc
356
467
578
689
7910
81011
  • 使用isin函数根据特定值筛选记录。筛选a值等于3或者5的记录
df[df.a.isin([3, 5])]

 
 
  • 1
abc
2345
4567
1.2 多条件筛选

可以使用&(并)|(或)操作符或者特定的函数实现多条件筛选

  • 使用&筛选a列的取值大于3,b列的取值大于6的记录
df[(df['a'] > 3) & (df['b'] > 6)]

 
 
  • 1
abc
5678
6789
78910
891011
  • 使用numpylogical_and函数完成同样的功能
df[np.logical_and(df['a']> 3,df['b']>6)]

 
 
  • 1
abc
5678
6789
78910
891011
1.3 排除特定行

筛选特定行做起来很方便,可以使用特定的函数完成,但是排除含特定值的行就需要做一些变通了。

例如,我们选出a列的值不等于3或者5的记录。基本的做法是将a列选择出来,把值3和5剔除,再使用isin函数。

ex_list = list(df['a'])

ex_list.remove(3)
ex_list.remove(5)

  • 1
  • 2
  • 3
  • 4
ex_list

 
 
  • 1
[1, 2, 4, 6, 7, 8, 9]

 
 
  • 1
df[df.a.isin(ex_list)]

 
 
  • 1
abc
0123
1234
3456
5678
6789
78910
891011

2. 索引筛选

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-M2hSGR4F-1571379379074)(E:\CQUPT\AI\python\jupyter notebook\Python数据科学\picture\iloc、loc、ix的使用(列切片及行切片).png)]

2.1 切片操作
# 使用切片操作选择特定的行
df[1:4]

 
 
  • 1
  • 2
abc
1234
2345
3456
# 传入列名选择特定的列
df[['a','c']]

 
 
  • 1
  • 2
ac
013
124
235
346
457
568
679
7810
8911
2.2 loc函数

当每列已有column name时,用 df ['a']就能选取出一整列数据。如果你知道column names和index(这里df的index没有指定,是默认生成的下标),且两者都很好输入,可以选择.loc同时进行行列选择。

df.loc[0,'c']

 
 
  • 1
3

 
 
  • 1
df.loc[1:4,['a','c']]

 
 
  • 1
ac
124
235
346
457
df.loc[[1,3,5],['a','c']]

 
 
  • 1
ac
124
346
568
2.3 iloc

如果column name太长,输入不方便,或者index是一列时间序列,更不好输入,那就可以选择 .iloc了,该方法接受列名的index,iloc使得我们可以对column使用slice(切片)的方法对数据进行选取。这边的 i 我觉得代表index,比较好记点。

df.iloc[0,2]

 
 
  • 1
3

 
 
  • 1
df.iloc[1:4,[0,2]]  # .iloc方法里面区间是前闭后开? .loc方法里面是闭区间?

 
 
  • 1
ac
124
235
346
df.iloc[[1,3,5],[0,2]]

 
 
  • 1
ac
124
346
568
df.iloc[[1,3,5],0:2]

 
 
  • 1
ab
123
345
567
2.4 ix函数

ix的功能更加强大,参数既可以是索引,也可以是名称,相当于,lociloc的合体。需要注意的是在使用的时候需要统一,在行选择时同时出现索引和名称, 同样在同行选择时同时出现索引和名称。

df.ix[1:3,['a','b']]

 
 
  • 1
e:\anaconda3.5\lib\site-packages\ipykernel_launcher.py:1: DeprecationWarning: 
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing

See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/indexing.html#ix-indexer-is-deprecated
“”"Entry point for launching an IPython kernel.

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
ab
123
234
345
df.ix[[1,3,5],['a','b']]

 
 
  • 1
ab
123
345
567
df.ix[[1,3,5],[0,2]]

 
 
  • 1
ac
124
346
568
2.5 at函数

根据指定行index及列label,快速定位DataFrame的元素,选择列时仅支持列名

df.at[3,'a']

 
 
  • 1
4

 
 
  • 1
2.6 iat函数

与at的功能相同,只使用索引参数。

df.iat[3,0]

 
 
  • 1
4

 
 
  • 1
df.iat[3,'a']

 
 
  • 1
---------------------------------------------------------------------------

ValueError Traceback (most recent call last)

<ipython-input-22-e644c9fe0a65> in <module>
----> 1 df.iat[3,‘a’]

e:\anaconda3.5\lib\site-packages\pandas\core\indexing.py in getitem(self, key)
1783 raise ValueError(‘Invalid call for scalar access (getting)!’)
1784
-> 1785 key = self._convert_key(key)
1786 return self.obj.get_value(*key, takeable=self._takeable)
1787

e:\anaconda3.5\lib\site-packages\pandas\core\indexing.py in _convert_key(self, key, is_setter)
1852 for a, i in zip(self.obj.axes, key):
1853 if not is_integer(i):
-> 1854 raise ValueError("iAt based indexing can only have integer "
1855 “indexers”)
1856 return key

ValueError: iAt based indexing can only have integer indexers

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
pandas是一个强大的数据分析工具,在数据处理过程中,我们经常需要对DataFrame进行查询操作。Pandas提供了多种查询方法,包括lociloc、at、iatix。以下是对这些方法的详细解释和区别。 1. locloc是通过行标签和列标签进行查询的方法。例如,可以使用df.loc[row_index, column_name]的方式来查询DataFrame的特定值或行列的子集。其中row_index可以是单个索引、索引列表或布尔数组,column_name可以是单个列名或列名列表。loc方法返回的是一个新的DataFrame或Series对象,以便进行进一步的操作。 2. ilociloc是通过行索引和列索引进行查询的方法。它与loc的用法类似,但是不是使用标签,而是使用整数位置来定位数据。例如,可以使用df.iloc[row_index, column_index]的方式来查询DataFrame的特定值或行列的子集。同样地,iloc方法返回的也是一个新的DataFrame或Series对象。 3. at:at用于直接查询DataFrame中的单个元素,它使用行标签和列标签来定位。例如,可以使用df.at[row_label, column_label]的方式来获取特定位置的值。与loc方法相比,at方法更快,并且只返回标量值。 4. iatiat用于通过整数位置来查询DataFrame中的单个元素。可以使用df.iat[row_index, column_index]的方式来获取特定位置的值。iat方法与at方法的区别与ilocloc的区别相似。 5. ix:在旧版本的pandas中,ix方法用于混合标签和整数位置的查询。它可以使用标签或整数位置来定位数据,但是由于存在一些歧义和性能问题,自从pandas 0.20版本后,被推荐使用lociloc方法来替代ix方法。 总的来说,loc和at是通过标签进行查询的方法,ilociat是通过整数位置进行查询的方法。同时,lociloc返回的是一个新的DataFrame或Series对象,而at和iat只返回标量值。在实际使用中,根据需要选择不同的查询方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值