在本章中,咱們將討論如何切割和丟棄日期,並獲取Pandas中大對象的子集。python
Python和NumPy索引運算符"[]"
和屬性運算符"."
。 能夠在普遍的用例中快速輕鬆地訪問Pandas數據結構。然而,因爲要訪問的數據類型不是預先知道的,因此直接使用標準運算符具備一些優化限制。對於生產環境的代碼,咱們建議利用本章介紹的優化Pandas數據訪問方法。shell
Pandas如今支持三種類型的多軸索引; 這三種類型在下表中提到 -swift
編號 | 索引 | 描述 |
---|---|---|
1 | .loc() |
基於標籤 |
2 | .iloc() |
基於整數 |
3 | .ix() |
基於標籤和整數 |
Pandas提供了各類方法來完成基於標籤的索引。 切片時,也包括起始邊界。整數是有效的標籤,但它們是指標籤而不是位置。數組
.loc()
具備多種訪問方式,如 -數據結構
loc
須要兩個單/列表/範圍運算符,用","
分隔。第一個表示行,第二個表示列。dom
示例1優化
#import the pandas library and aliasing as pd import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(8, 4), index = ['a','b','c','d','e','f','g','h'], columns = ['A', 'B', 'C', 'D']) #select all rows for a specific column print (df.loc[:,'A'])
執行上面示例代碼,獲得如下結果 -google
a 0.015860 b -0.014135 c 0.446061 d 1.801269 e -1.404779 f -0.044016 g 0.996651 h 0.764672 Name: A, dtype: float64
示例2spa
import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(8, 4), index = ['a','b','c','d','e','f','g','h'], columns = ['A', 'B', 'C', 'D']) # Select all rows for multiple columns, say list[] print (df.loc[:,['A','C']])
執行上面示例代碼,獲得如下結果 -code
A C a -0.529735 -1.067299 b -2.230089 -1.798575 c 0.685852 0.333387 d 1.061853 0.131853 e 0.990459 0.189966 f 0.057314 -0.370055 g 0.453960 -0.624419 h 0.666668 -0.433971
示例3
import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(8, 4), index = ['a','b','c','d','e','f','g','h'], columns = ['A', 'B', 'C', 'D']) # Select few rows for multiple columns, say list[] print (df.loc[['a','b','f','h'],['A','C']]) # Select all rows for multiple columns, say list[] print (df.loc[:,['A','C']])
執行上面示例代碼,獲得如下結果 -
A C a -1.959731 0.720956 b 1.318976 0.199987 f -1.117735 -0.181116 h -0.147029 0.027369 A C a -1.959731 0.720956 b 1.318976 0.199987 c 0.839221 -1.611226 d 0.722810 1.649130 e -0.524845 -0.037824 f -1.117735 -0.181116 g -0.642907 0.443261 h -0.147029 0.027369
示例4
# import the pandas library and aliasing as pd import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(8, 4), index = ['a','b','c','d','e','f','g','h'], columns = ['A', 'B', 'C', 'D']) # Select range of rows for all columns print (df.loc['a':'h'])
執行上面示例代碼,獲得如下結果 -
A B C D a 1.556186 1.765712 1.060657 0.810279 b 1.377965 -0.183283 -0.224379 0.963105 c -0.530016 0.167183 -0.066459 0.074198 d -1.515189 -1.453529 -1.559400 1.072148 e -0.487399 0.436143 -1.045622 -0.029507 f 0.552548 0.410745 0.570222 -0.628133 g 0.865293 -0.638388 0.388827 -0.469282 h -0.690596 1.765139 -0.492070 -0.176074
示例5
import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(8, 4), index = ['a','b','c','d','e','f','g','h'], columns = ['A', 'B', 'C', 'D']) # for getting values with a boolean array print (df.loc['a']>0)
執行上面示例代碼,獲得如下結果 -
A False B True C False D True Name: a, dtype: bool
Pandas提供了各類方法,以得到純整數索引。像python和numpy同樣,第一個位置是基於0
的索引。
各類訪問方式以下 -
示例1
import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(8, 4), columns = ['A', 'B', 'C', 'D']) # select all rows for a specific column print (df.iloc[:4])
執行上面示例代碼,獲得如下結果 -
A B C D 0 0.277146 0.274234 0.860555 -1.312323 1 -1.064776 2.082030 0.695930 2.409340 2 0.033953 -1.155217 0.113045 -0.028330 3 0.241075 -2.156415 0.939586 -1.670171
示例2
import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(8, 4), columns = ['A', 'B', 'C', 'D']) # Integer slicing print (df.iloc[:4]) print (df.iloc[1:5, 2:4])
執行上面示例代碼,獲得如下結果 -
A B C D 0 1.346210 0.251839 0.975964 0.319049 1 0.459074 0.038155 0.893615 0.659946 2 -1.097043 0.017080 0.869331 -1.443731 3 1.008033 -0.189436 -0.483688 -1.167312 C D 1 0.893615 0.659946 2 0.869331 -1.443731 3 -0.483688 -1.167312 4 1.566395 -1.292206
示例3
import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(8, 4), columns = ['A', 'B', 'C', 'D']) # Slicing through list of values print (df.iloc[[1, 3, 5], [1, 3]]) print (df.iloc[1:3, :]) print (df.iloc[:,1:3])
執行上面示例代碼,獲得如下結果 -
B D 1 0.081257 0.009109 3 1.037680 -1.467327 5 1.106721 0.320468 A B C D 1 -0.133711 0.081257 -0.031869 0.009109 2 0.895576 -0.513450 -0.048573 0.698965 B C 0 0.442735 -0.949859 1 0.081257 -0.031869 2 -0.513450 -0.048573 3 1.037680 -0.801157 4 -0.547456 -0.255016 5 1.106721 0.688142 6 -0.466452 0.219914 7 1.583112 0.982030
除了基於純標籤和整數以外,Pandas還提供了一種使用.ix()
運算符進行選擇和子集化對象的混合方法。
示例1
import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(8, 4), columns = ['A', 'B', 'C', 'D']) # Integer slicing print (df.ix[:4])
執行上面示例代碼,獲得如下結果 -
A B C D 0 -1.449975 -0.002573 1.349962 0.539765 1 -1.249462 -0.800467 0.483950 0.187853 2 1.361273 -1.893519 0.307613 -0.119003 3 -0.103433 -1.058175 -0.587307 -0.114262 4 -0.612298 0.873136 -0.607457 1.047772
示例2
import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(8, 4), columns = ['A', 'B', 'C', 'D']) # Index slicing print (df.ix[:,'A'])
執行上面示例代碼,獲得如下結果 -
0 1.539915 1 1.359477 2 0.239694 3 0.563254 4 2.123950 5 0.341554 6 -0.075717 7 -0.606742 Name: A, dtype: float64
使用多軸索引從Pandas對象獲取值可以使用如下符號 -
對象 | 索引 | 描述 |
---|---|---|
Series | s.loc[indexer] |
標量值 |
DataFrame | df.loc[row_index,col_index] |
標量對象 |
Panel | p.loc[item_index,major_index, minor_index] |
p.loc[item_index,major_index, minor_index] |
注意 -
.iloc()
和.ix()
應用相同的索引選項和返回值。
如今來看看如何在DataFrame對象上執行每一個操做。這裏使用基本索引運算符[]
-
示例1
import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(8, 4), columns = ['A', 'B', 'C', 'D']) print (df['A'])
執行上面示例代碼,獲得如下結果 -
0 0.028277 1 -1.037595 2 -0.563495 3 -1.196961 4 -0.805250 5 -0.911648 6 -0.355171 7 -0.232612 Name: A, dtype: float64
示例2
import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(8, 4), columns = ['A', 'B', 'C', 'D']) print (df[['A','B']])
執行上面示例代碼,獲得如下結果 -
A B 0 -0.767339 -0.729411 1 -0.563540 -0.639142 2 0.873589 -2.166382 3 0.900330 0.253875 4 -0.520105 0.064438 5 -1.452176 -0.440864 6 -0.291556 -0.861924 7 -1.464235 0.313168
示例3
import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(8, 4), columns = ['A', 'B', 'C', 'D']) print (df[2:2])
執行上面示例代碼,獲得如下結果 -
Empty DataFrame Columns: [A, B, C, D] Index: []
可使用屬性運算符.
來選擇列。
示例
import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(8, 4), columns = ['A', 'B', 'C', 'D']) print (df.A)
執行上面示例代碼,獲得如下結果 -
0 0.104820 1 -1.206600 2 0.469083 3 -0.821226 4 -1.238865 5 1.083185 6 -0.827833 7 -0.199558 Name: A, dtype: float64