pandas-loc和iloc索引的对比

pandas读取数据后可以很方便的索引数据,其中我们可以直接从pandas数据读取,例如:
df = pd.DataFrame({'A': ['a', 'b', 'a'], 'B': ['b', 'a', 'c'],'C': [1, 2, 3]})      
df
Out[54]: 
   A  B  C
0  a  b  1
1  b  a  2
2  a  c  3
df["A"]
Out[56]: 
0    a
1    b
2    a
Name: A, dtype: object
#选多列,列名一定要用[]括起来
df[["A","B"]]
Out[58]: 
   A  B
0  a  b
1  b  a
2  a  c
df.loc[:,["A","B"]]   #loc可以按label索引
Out[63]: 
   A  B
0  a  b
1  b  a
2  a  c
df.iloc[:,[1,2]]
Out[65]: 
   B  C
0  b  1
1  a  2
2  c  3
df.iloc[:,[0,2]]
Out[66]: 
   A  C
0  a  1
1  b  2
2  a  3
df.iloc[:,1:3]  #对列切片
Out[72]: 
   B  C
0  b  1
1  a  2
2  c  3

pandas还提供了索引,包括loc和iloc以及ix等,ix在新版本启用,因此我们只考虑loc和iloc

新引入一个例子

--------------分割线----------------------------------------------

>>> s = pd.Series(np.nan, index=[49,48,47,46,45, 1, 2, 3, 4, 5])
>>> s
49   NaN
48   NaN
47   NaN
46   NaN
45   NaN
1    NaN
2    NaN
3    NaN
4    NaN
5    NaN
>>> s.iloc[:3] # slice the first three rows 
49   NaN        #index ==3
48   NaN
47   NaN

>>> s.loc[:3] # slice up to and including label 3 
49   NaN      #切到标签3为止
48   NaN
47   NaN
46   NaN
45   NaN
1    NaN
2    NaN
3    NaN

如果label不存在,那么loc报错

>>> s.iloc[:6]
49   NaN
48   NaN
47   NaN
46   NaN
45   NaN
1    NaN

>>> s.loc[:6]
KeyError: 6

----另一个例子----------------------

>>> df = pd.DataFrame(np.nan, 
                      index=list('abcde'),
                      columns=['x','y','z', 8, 9])
>>> df
    x   y   z   8   9
a NaN NaN NaN NaN NaN
b NaN NaN NaN NaN NaN
c NaN NaN NaN NaN NaN
d NaN NaN NaN NaN NaN
e NaN NaN NaN NaN NaN
>>> df.iloc[:df.index.get_loc('c') + 1, :4]
    x   y   z   8
a NaN NaN NaN NaN
b NaN NaN NaN NaN
c NaN NaN NaN NaN

参考:https://stackoverflow.com/questions/31593201/pandas-iloc-vs-ix-vs-loc-explanation-how-are-they-different

猜你喜欢

转载自blog.csdn.net/Dawei_01/article/details/80424452