淺談pandas中Dataframe的查詢(xún)方法([], loc, iloc, at, iat, ix)
pandas為我們提供了多種切片方法,而要是不太了解這些方法,就會(huì)經(jīng)常容易混淆。下面舉例對(duì)這些切片方法進(jìn)行說(shuō)明。
數(shù)據(jù)介紹
先隨機(jī)生成一組數(shù)據(jù):
In [5]: rnd_1 = [random.randrange(1,20) for x in xrange(1000)]
...: rnd_2 = [random.randrange(1,20) for x in xrange(1000)]
...: rnd_3 = [random.randrange(1,20) for x in xrange(1000)]
...: fecha = pd.date_range('2012-4-10', '2015-1-4')
...:
...: data = pd.DataFrame({'fecha':fecha, 'rnd_1': rnd_1, 'rnd_2': rnd_2, 'rnd_3': rnd_3})
In [6]: data.describe()
Out[6]:
rnd_1 rnd_2 rnd_3
count 1000.000000 1000.000000 1000.000000
mean 9.946000 9.825000 9.894000
std 5.553911 5.559432 5.423484
min 1.000000 1.000000 1.000000
25% 5.000000 5.000000 5.000000
50% 10.000000 10.000000 10.000000
75% 15.000000 15.000000 14.000000
max 19.000000 19.000000 19.000000
[]切片方法
使用方括號(hào)能夠?qū)ataFrame進(jìn)行切片,有點(diǎn)類(lèi)似于python的列表切片。按照索引能夠?qū)崿F(xiàn)行選擇或列選擇或區(qū)塊選擇。
# 行選擇
In [7]: data[1:5]
Out[7]:
fecha rnd_1 rnd_2 rnd_3
1 2012-04-11 1 16 3
2 2012-04-12 7 6 1
3 2012-04-13 2 16 7
4 2012-04-14 4 17 7
# 列選擇
In [10]: data[['rnd_1', 'rnd_3']]
Out[10]:
rnd_1 rnd_3
0 8 12
1 1 3
2 7 1
3 2 7
4 4 7
5 12 8
6 2 12
7 9 8
8 13 17
9 4 7
10 14 14
11 19 16
12 2 12
13 15 18
14 13 18
15 13 11
16 17 7
17 14 10
18 9 6
19 11 15
20 16 13
21 18 9
22 1 18
23 4 3
24 6 11
25 2 13
26 7 17
27 11 8
28 3 12
29 4 2
.. ... ...
970 8 14
971 19 5
972 13 2
973 8 10
974 8 17
975 6 16
976 3 2
977 12 6
978 12 10
979 15 13
980 8 4
981 17 3
982 1 17
983 11 5
984 7 7
985 13 14
986 6 19
987 13 9
988 3 15
989 19 6
990 7 11
991 11 7
992 19 12
993 2 15
994 10 4
995 14 13
996 12 11
997 11 15
998 17 14
999 3 8
[1000 rows x 2 columns]
# 區(qū)塊選擇
In [11]: data[:7][['rnd_1', 'rnd_2']]
Out[11]:
rnd_1 rnd_2
0 8 17
1 1 16
2 7 6
3 2 16
4 4 17
5 12 19
6 2 7
不過(guò)對(duì)于多列選擇,不能像行選擇時(shí)一樣使用1:5這樣的方法來(lái)選擇。
In [12]: data[['rnd_1':'rnd_3']]
File "<ipython-input-13-6291b6a83eb0>", line 1
data[['rnd_1':'rnd_3']]
^
SyntaxError: invalid syntax
loc
loc可以讓你按照索引來(lái)進(jìn)行行列選擇。
In [13]: data.loc[1:5]
Out[13]:
fecha rnd_1 rnd_2 rnd_3
1 2012-04-11 1 16 3
2 2012-04-12 7 6 1
3 2012-04-13 2 16 7
4 2012-04-14 4 17 7
5 2012-04-15 12 19 8
這里需要注意的是,loc與第一種方法不同之處在于會(huì)把第5行也選擇進(jìn)去,而第一種方法只會(huì)選擇到第4行為止。
data.loc[2:4, ['rnd_2', 'fecha']] Out[14]: rnd_2 fecha 2 6 2012-04-12 3 16 2012-04-13 4 17 2012-04-14
loc能夠選擇在兩個(gè)特定日期之間的數(shù)據(jù),需要注意的是這兩個(gè)日期必須都要在索引中。
In [15]: data_fecha = data.set_index('fecha')
...: data_fecha.head()
Out[15]:
rnd_1 rnd_2 rnd_3
fecha
2012-04-10 8 17 12
2012-04-11 1 16 3
2012-04-12 7 6 1
2012-04-13 2 16 7
2012-04-14 4 17 7
In [16]: # 生成兩個(gè)特定日期
...: fecha_1 = dt.datetime(2013, 4, 14)
...: fecha_2 = dt.datetime(2013, 4, 18)
...:
...: # 生成切片數(shù)據(jù)
...: data_fecha.loc[fecha_1: fecha_2]
Out[16]:
rnd_1 rnd_2 rnd_3
fecha
2013-04-14 17 10 5
2013-04-15 14 4 9
2013-04-16 1 2 18
2013-04-17 9 15 1
2013-04-18 16 7 17
更新:如果沒(méi)有特殊需求,強(qiáng)烈建議使用loc而盡量少使用[],因?yàn)閘oc在對(duì)DataFrame進(jìn)行重新賦值操作時(shí)會(huì)避免chained indexing問(wèn)題,使用[]時(shí)編譯器很可能會(huì)給出SettingWithCopy的警告。
具體可以參見(jiàn)官方文檔:http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
iloc
如果說(shuō)loc是按照索引(index)的值來(lái)選取的話(huà),那么iloc就是按照索引的位置來(lái)進(jìn)行選取。iloc不關(guān)心索引的具體值是多少,只關(guān)心位置是多少,所以使用iloc時(shí)方括號(hào)中只能使用數(shù)值。
# 行選擇
In [17]: data_fecha[10: 15]
Out[17]:
rnd_1 rnd_2 rnd_3
fecha
2012-04-20 14 6 14
2012-04-21 19 14 16
2012-04-22 2 6 12
2012-04-23 15 8 18
2012-04-24 13 8 18
# 列選擇
In [18]: data_fecha.iloc[:,[1,2]].head()
Out[18]:
rnd_2 rnd_3
fecha
2012-04-10 17 12
2012-04-11 16 3
2012-04-12 6 1
2012-04-13 16 7
2012-04-14 17 7
# 切片選擇
In [19]: data_fecha.iloc[[1,12,34],[0,2]]
Out[19]:
rnd_1 rnd_3
fecha
2012-04-11 1 3
2012-04-22 2 12
2012-05-14 17 10
at
at的使用方法與loc類(lèi)似,但是比loc有更快的訪(fǎng)問(wèn)數(shù)據(jù)的速度,而且只能訪(fǎng)問(wèn)單個(gè)元素,不能訪(fǎng)問(wèn)多個(gè)元素。
In [20]: timeit data_fecha.at[fecha_1,'rnd_1'] The slowest run took 3783.11 times longer than the fastest. This could mean that an intermediate result is being cached. 100000 loops, best of 3: 11.3 µs per loop In [21]: timeit data_fecha.loc[fecha_1,'rnd_1'] The slowest run took 121.24 times longer than the fastest. This could mean that an intermediate result is being cached. 10000 loops, best of 3: 192 µs per loop In [22]: data_fecha.at[fecha_1,'rnd_1'] Out[22]: 17
iat
iat對(duì)于iloc的關(guān)系就像at對(duì)于loc的關(guān)系,是一種更快的基于索引位置的選擇方法,同at一樣只能訪(fǎng)問(wèn)單個(gè)元素。
In [23]: data_fecha.iat[1,0] Out[23]: 1 In [24]: timeit data_fecha.iat[1,0] The slowest run took 6.23 times longer than the fastest. This could mean that an intermediate result is being cached. 100000 loops, best of 3: 8.77 µs per loop In [25]: timeit data_fecha.iloc[1,0] 10000 loops, best of 3: 158 µs per loop
ix
以上說(shuō)過(guò)的幾種方法都要求查詢(xún)的秩在索引中,或者位置不超過(guò)長(zhǎng)度范圍,而ix允許你得到不在DataFrame索引中的數(shù)據(jù)。
In [28]: date_1 = dt.datetime(2013, 1, 10, 8, 30)
...: date_2 = dt.datetime(2013, 1, 13, 4, 20)
...:
...: # 生成切片數(shù)據(jù)
...: data_fecha.ix[date_1: date_2]
Out[28]:
rnd_1 rnd_2 rnd_3
fecha
2013-01-11 19 17 19
2013-01-12 10 9 17
2013-01-13 15 3 10
如上面的例子所示,2013年1月10號(hào)并沒(méi)有被選擇進(jìn)去,因?yàn)檫@個(gè)時(shí)間點(diǎn)被看作為0點(diǎn)0分,比8點(diǎn)30分要早一些。
以上這篇淺談pandas中Dataframe的查詢(xún)方法([], loc, iloc, at, iat, ix)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Python pymysql連接數(shù)據(jù)庫(kù)并將查詢(xún)結(jié)果轉(zhuǎn)化為Pandas dataframe
- Python數(shù)據(jù)分析之?Pandas?Dataframe修改和刪除及查詢(xún)操作
- python將Dataframe格式的數(shù)據(jù)寫(xiě)入opengauss數(shù)據(jù)庫(kù)并查詢(xún)
- python pymysql鏈接數(shù)據(jù)庫(kù)查詢(xún)結(jié)果轉(zhuǎn)為Dataframe實(shí)例
- 詳解pandas DataFrame的查詢(xún)方法(loc,iloc,at,iat,ix的用法和區(qū)別)
- DataFrame數(shù)據(jù)框模糊查詢(xún)與去重方式
相關(guān)文章
在Python中利用Into包整潔地進(jìn)行數(shù)據(jù)遷移的教程
這篇文章主要介紹了在Python中如何利用Into包整潔地進(jìn)行數(shù)據(jù)遷移,在數(shù)據(jù)格式的任意兩個(gè)格式之間高效地遷移數(shù)據(jù),需要的朋友可以參考下2015-03-03
Python3 把一個(gè)列表按指定數(shù)目分成多個(gè)列表的方式
今天小編就為大家分享一篇Python3 把一個(gè)列表按指定數(shù)目分成多個(gè)列表的方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12
Python從ZabbixAPI獲取信息及實(shí)現(xiàn)Zabbix-API 監(jiān)控的方法
這篇文章主要介紹了Python從ZabbixAPI獲取信息及實(shí)現(xiàn)Zabbix-API 監(jiān)控的方法,需要的朋友可以參考下2018-09-09
python基礎(chǔ)之類(lèi)屬性和實(shí)例屬性
這篇文章主要介紹了python類(lèi)屬性和實(shí)例屬性,實(shí)例分析了Python中返回一個(gè)返回值與多個(gè)返回值的方法,需要的朋友可以參考下2021-10-10
Python中urllib2模塊的8個(gè)使用細(xì)節(jié)分享
這篇文章主要介紹了Python中urllib2模塊的8個(gè)使用細(xì)節(jié)分享,本文講解了Proxy設(shè)置、Timeout設(shè)置、加入特定Header、Redirect、Cookie、PUT和DELETE方法等內(nèi)容,需要的朋友可以參考下2015-01-01
使用python實(shí)現(xiàn)時(shí)間序列白噪聲檢驗(yàn)方式
這篇文章主要介紹了使用python實(shí)現(xiàn)時(shí)間序列白噪聲檢驗(yàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06

