pandas 索引选取和过滤(四)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/luoganttcc/article/details/69240481

    建立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]: 1

obj[1]
Out[7]: 1

obj[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']=5

obj
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]=0

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

    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    15

data.ix[data.three>5,:3]
Out[14]: 
          one  two  three
colorado    4    5      6
utah        8    9     10
new york   12   13     14
阅读更多
换一批

没有更多推荐了,返回首页