pandas 索引选取和过滤(四)
生活随笔
收集整理的這篇文章主要介紹了
pandas 索引选取和过滤(四)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
????建立Series
from pandas import Series,DataFrame import numpy as np import pandas as pd obj=Series(np.arange(4),index=['a','b','c','d'])obj Out[5]: a 0 b 1 c 2 d 3 dtype: int32????選取
obj['b'] Out[6]: 1obj[1] Out[7]: 1obj[3] Out[8]: 3 obj[2:4] Out[10]: c 2 d 3 dtype: int32 obj[['b','a','d']] Out[11]: b 1 a 0 d 3 dtype: int32 obj[[1,3]] Out[12]: b 1 d 3 dtype: int32 obj[obj<2] Out[13]: a 0 b 1 dtype: int32????利用標簽切片與普通python 切片不同,python是左閉右開區間[a,b),而標簽切片是閉合區間[a,b]
In [14]: obj['b':'c'] Out[14]: b 1 c 2 dtype: int32 obj['b','c']=5obj Out[16]: a 0 b 5 c 5 d 3 dtype: int32????DataFrame 選取
data=DataFrame(np.arange(16).reshape((4,4)),index=['ohio','colorado','utah','new york'],columns=['one','two','three','four']) data Out[18]: one two three four ohio 0 1 2 3 colorado 4 5 6 7 utah 8 9 10 11 new york 12 13 14 15 data['two'] Out[19]: ohio 1 colorado 5 utah 9 new york 13 Name: two, dtype: int32 data[['three','one']] Out[20]: three one ohio 2 0 colorado 6 4 utah 10 8 new york 14 12 data[:2] Out[21]: one two three four ohio 0 1 2 3 colorado 4 5 6 7 data[data['three']>5] Out[22]: one two three four colorado 4 5 6 7 utah 8 9 10 11 new york 12 13 14 15 data<5 Out[23]: one two three four ohio True True True True colorado True False False False utah False False False False new york False False False False data[data<5]=0data Out[26]: one two three four ohio 0 0 0 0 colorado 0 5 6 7 utah 8 9 10 11 new york 12 13 14 15????ix的用法
data Out[26]: one two three four ohio 0 0 0 0 colorado 0 5 6 7 utah 8 9 10 11 new york 12 13 14 15 data.ix['colorado',['two','three']] Out[27]: two 5 three 6 Name: colorado, dtype: int32 data.ix[['colorado','utah'],[3,0,1]] Out[29]: four one two colorado 7 0 5 utah 11 8 9 data.ix[2] Out[6]: one 8 two 9 three 10 four 11 Name: utah, dtype: int32 data.ix[:'utah','two'] Out[9]: ohio 1 colorado 5 utah 9 Name: two, dtype: int32 data.ix[data.three>5] Out[13]: one two three four colorado 4 5 6 7 utah 8 9 10 11 new york 12 13 14 15data.ix[data.three>5,:3] Out[14]: one two three colorado 4 5 6 utah 8 9 10 new york 12 13 14總結
以上是生活随笔為你收集整理的pandas 索引选取和过滤(四)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pandas Series DataFr
- 下一篇: python 数组基本用法