python數據處理相關操作——iloc、loc、ix選取數據


python數據處理相關操作——選取數據

iloc,loc,ix

import pandas as pd
from pandas import DataFrame

創建數據框

data = {'a':[11,22,33,44],
       'b':['aa','bb','cc','dd'],
       'c':[9,8,7,6],
        'd':[1,2,3,4]
       }
df = DataFrame(data)
df
a b c d
0 11 aa 9 1
1 22 bb 8 2
2 33 cc 7 3
3 44 dd 6 4

iloc:通過行/列號選取數據

df.iloc[0] #選取第0行數據
a    11
b    aa
c     9
d     1
Name: 0, dtype: object
df.iloc[0:2] #選取多行
a b c d
0 11 aa 9 1
1 22 bb 8 2
df.iloc[:,[1]] #也可以按照列號選取某列 選取第2列
b
0 aa
1 bb
2 cc
3 dd
df.iloc[0:1,[1]] #可以按照行號選取某行某列 選取第0行 第2列
b
0 aa
df.iloc[0:2,[0,1]] #可以按照行號選取多行多列 選取第0~2行 第0~2列
a b
0 11 aa
1 22 bb

loc通過標簽選取數據

df.loc[0] #選取第1行 因為第1行的行號是0所以和iloc效果相同
a    11
b    aa
c     9
d     1
Name: 0, dtype: object
data = {'a':[11,22,33,44],
       'b':['aa','bb','cc','dd'],
       'c':[9,8,7,6],
        'd':[1,2,3,4]
       }
df1 = DataFrame(data,index = ['a','b','c','d'])
df1
a b c d
a 11 aa 9 1
b 22 bb 8 2
c 33 cc 7 3
d 44 dd 6 4
df1.loc['b'] #選取第b行
a    22
b    bb
c     8
d     2
Name: b, dtype: object
df1.loc['b':] #選取多行
a b c d
b 22 bb 8 2
c 33 cc 7 3
d 44 dd 6 4
df1.loc[:,['a']] #通過標簽選取某列
a
a 11
b 22
c 33
d 44
df1.loc[:,['a','b']] #通過標簽選取多列
a b
a 11 aa
b 22 bb
c 33 cc
d 44 dd
df1.loc['a',['b','c']] #通過標簽選取某行某列
b    aa
c     9
Name: a, dtype: object

按照條件選取數據

df1.loc[df1['a']==11] #通過單個條件選取數據
a b c d
a 11 aa 9 1
df1.loc[(df1['a']==11)&(df1['d']==1)] #通過單多個條件選取數據
a b c d
a 11 aa 9 1

ix 簡單粗暴 混合使用

也就是說 ix把iloc和loc語法綜合了,愛用哪個用哪個,不過會報個warning

df #再看下dataframe
a b c d
0 11 aa 9 1
1 22 bb 8 2
2 33 cc 7 3
3 44 dd 6 4
df.ix[1] #可以像iloc通過行號選取
/Users/anaconda/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:1: FutureWarning: 
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing

See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#ix-indexer-is-deprecated
  """Entry point for launching an IPython kernel.





a    22
b    bb
c     8
d     2
Name: 1, dtype: object
df1
a b c d
a 11 aa 9 1
b 22 bb 8 2
c 33 cc 7 3
d 44 dd 6 4
df1.ix['a'] #可以像loc通過標簽選取
/Users/anaconda/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:1: FutureWarning: 
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing

See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#ix-indexer-is-deprecated
  """Entry point for launching an IPython kernel.





a    11
b    aa
c     9
d     1
Name: a, dtype: object
df1.ix[3,3] #通過行號選取指定位置的數據
/Users/anaconda/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:1: FutureWarning: 
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing

See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#ix-indexer-is-deprecated
  """Entry point for launching an IPython kernel.
/Users/anaconda/anaconda3/lib/python3.7/site-packages/pandas/core/indexing.py:961: FutureWarning: 
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing

See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#ix-indexer-is-deprecated
  return getattr(section, self.name)[new_key]





4
df1.ix['a','a']  #通過標簽選取指定位置的數據
/Users/anaconda/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:1: FutureWarning: 
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing

See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#ix-indexer-is-deprecated
  """Entry point for launching an IPython kernel.





11


免責聲明!

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



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