pandas 刪除、索引及切片


一、軸向上刪除條目

通過drop方法,可以刪除Series的一個元素,或者DataFrame的一行或一列。默認情況下,drop方法按行刪除,且不會修改原數據,但指定axis=1則按列刪除,指定inplace=True則修改原數據。

In [9]: s= pd.Series(np.arange(5),index=list('abcde')) In [10]: s Out[10]: a 0 b 1 c 2 d 3 e 4 dtype: int32 In [11]: new_s = s.drop('c') In [12]: new_s Out[12]: a 0 b 1 d 3 e 4 dtype: int32 In [13]: s Out[13]: a 0 b 1 c 2 d 3 e 4 dtype: int32 In [14]: df = pd.DataFrame(np.arange(16).reshape(4,4),columns=['one','two','three','four']) In [15]: df Out[15]: one two three four 0 0 1      2     3
1    4    5      6     7
2    8    9     10    11
3   12   13     14    15 In [16]: df.drop(2) Out[16]: one two three four 0 0 1      2     3
1    4    5      6     7
3   12   13     14    15 In [17]: df.drop('two',axis = 1)  # 指定刪除列,而不是默認的行
Out[17]: one three four 0 0 2     3
1    4      6     7
2    8     10    11
3   12     14    15 In [18]: df Out[18]: one two three four 0 0 1      2     3
1    4    5      6     7
2    8    9     10    11
3   12   13     14    15 In [21]: df.drop(2,inplace=True)  #修改原數據
In [22]: df Out[22]: one two three four 0 0 1      2     3
1    4    5      6     7
3   12   13     14    15

二、索引和切片

Series的打印效果,讓我們感覺它像個二維表格,實際上它還是一維的,其索引和numpy的一維數組比較類似,但還是有點區別的。

In [23]: se = pd.Series(np.linspace(1,4,5),index=list('abcde')) In [24]: se Out[24]: a 1.00 b 1.75 c 2.50 d 3.25 e 4.00 dtype: float64 In [28]: se['b']    # 利用我們專門指定的索引進行檢索
Out[28]: 1.75 In [29]: se[2]  # 實際上默認還有一個從0開始的索引供我們使用
Out[29]: 2.5 In [30]: se[2:4] Out[30]: c 2.50 d 3.25 dtype: float64 In [31]: se[['b','a','d']]  # 根據索引順序,值進行相應的排序,而不是我們認為的按原來的順序
Out[31]: b 1.75 a 1.00 d 3.25 dtype: float64 In [32]: se[[1,3]]  # 左閉右開 ,千萬不要寫成se[1,3]
Out[32]: b 1.75 d 3.25 dtype: float64 In [33]: se[se>2] Out[33]: c 2.50 d 3.25 e 4.00 dtype: float64 In [35]: se['b':'c']  # 什么!居然是左閉右也閉!
Out[35]: b 1.75 c 2.50 dtype: float64 In [36]: se['b':'c'] = 6  # 這樣會修改原Series
In [37]: se Out[37]: a 1.00 b 6.00 c 6.00 d 3.25 e 4.00 dtype: float64

 注意:如果你的Series是顯式的整數索引,那么s[1]這樣的取值操作會使用顯式索引,而s[1:3]這樣的切片操作卻會使用隱式索引。

In [21]: s = pd.Series(['a','b','c'], index=[1,3,5]) In [22]: s Out[22]: 1 a 3 b 5 c dtype: object In [23]: s[1] Out[23]: 'a' In [24]: s[1:3] Out[24]: 3 b 5 c dtype: object

對於DataFrame這種二維表格,情況有點不太一樣,請務必注意!

核心思維:在DataFrame中,優先按列操作!

In [38]: df = pd.DataFrame(np.arange(16).reshape(4,4), index=list('abcd'),columns=['one','two','three','four']) In [39]: df Out[39]: one two three four a 0 1      2     3 b 4    5      6     7 c 8    9     10    11 d 12   13     14    15 In [40]: df['two']  # 對於DataFrame默認是按列索引
Out[40]: a 1 b 5 c 9 d 13 Name: two, dtype: int32 In [41]: df['b'] # KeyError,不能直接按行索引
In [43]: df[['one','four']] Out[43]: one four a 0 3 b 4     7 c 8    11 d 12    15 In [44]: df[:2]  # 什么!切片的時候居然是按行進行!
Out[44]: one two three four a 0 1      2     3 b 4    5      6     7 In [46]: df['c':'d']  # 注意閉合區間
Out[46]: one two three four c 8    9     10    11 d 12   13     14    15 In [47]: df ['one':'three']  # 試圖用列索引來切片,但明顯后台按行索引去找了,沒找到。
Out[47]: Empty DataFrame Columns: [one, two, three, four] Index: [] In [48]: df < 5 Out[48]: one two three four a True True True True b True False False False c False False False False d False False False False In [49]: df[df<5] = 0   # 這一波操作和numpy很類似
In [50]: df Out[50]: one two three four a 0 0 0 0 b 0 5      6     7 c 8    9     10    11 d 12   13     14    15

是不是覺得好難理解記憶?還是numpy那種索引方式更符合人的思維習慣?沒關系,Pandas考慮到了這一點,提供了類似numpy的行+列的索引標簽,也就是loc和iloc。這兩者差一個字母i。后者是以隱含的整數索引值來索引的,前者則使用你指定的顯式的索引來定位值。

In [50]: df Out[50]: one two three four a 0 0 0 0 b 0 5      6     7 c 8    9     10    11 d 12   13     14    15 In [51]: df.loc['b', ['two','four']]  # 使用顯式索引值,用逗號分隔行和列參數
Out[51]: two 5 four 7 Name: b, dtype: int32 In [53]: df.loc['b':, 'two':'four']  # 切片方式,注意區間
Out[53]: two three four b 5      6     7 c 9     10    11 d 13     14    15 In [54]: df.iloc[2, [3, 0, 1]]  # 用隱含的整數索引檢索,但是這個打印格式好別扭
Out[54]: four 11 one 8 two 9 Name: c, dtype: int32 In [55]: df.iloc[2] Out[55]: one 8 two 9 three 10 four 11 Name: c, dtype: int32 In [56]: df.iloc[1:,2:3] # 注意區間
Out[56]: three b 6 c 10 d 14 In [57]: df.iloc[:,:3][df.three>5]  # 先切片,再布爾判斷
Out[57]: one two three b 0 5      6 c 8    9     10 d 12   13     14


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM