pandas ix & iloc &loc 的联系和区别

参考了几个博客,做了以下整理,如有雷同,是我抄别人的。。

参考链接:https://blog.csdn.net/xw_classmate/article/details/51333646

https://blog.csdn.net/hecongqing/article/details/61927615

loc——通过行标签索引行数据 
iloc——通过行号索引行数据 
ix——通过行标签或者行号索引行数据(基于loc和iloc 的混合) 
同理,索引列数据也是如此!

举例说明: 
1、分别使用loc、iloc、ix 索引第一行的数据: 
(1)loc

import pandas as pd
data=[[1,2,3],[4,5,6]]
index=['a','b']#行号
columns=['c','d','e']#列号
df=pd.DataFrame(data,index=index,columns=columns)#生成一个数据框

#print df.loc['a']
'''
c    1
d    2
e    3
'''

print df.loc[0]
#这个就会出现错误
'''
TypeError: cannot do label indexing on <class 'pandas.indexes.base.Index'> 
with these indexers [1] of <type 'int'>
'''

(2)iloc

import pandas as pd
data=[[1,2,3],[4,5,6]]
index=['a','b']#行号
columns=['c','d','e']#列号
df=pd.DataFrame(data,index=index,columns=columns)#生成一个数据框

print df.iloc[0]
'''
c    1
d    2
e    3
'''
print df.iloc['a']
'''
TypeError: cannot do positional indexing on <class 'pandas.indexes.base.Index'> 
with these indexers [a] of <type 'str'>
'''

(3)ix

import pandas as pd
data=[[1,2,3],[4,5,6]]
index=['a','b']#行号
columns=['c','d','e']#列号
df=pd.DataFrame(data,index=index,columns=columns)#生成一个数据框

print df.ix[0]
'''
c    1
d    2
e    3
'''
print df.ix['a']
'''
c    1
d    2
e    3
'''

2、分别使用loc、iloc、ix 索引第一列的数据:

import pandas as pd
data=[[1,2,3],[4,5,6]]
index=['a','b']#行号
columns=['c','d','e']#列号
df=pd.DataFrame(data,index=index,columns=columns)#生成一个数据框

print df.loc[:,['c']]

print df.iloc[:,[0]]

print df.ix[:,['c']]

print df.ix[:,[0]]
#结果都为
'''
   c
a  1
b  4
'''

3、分别使用loc、iloc、ix 索引多行的数据:

import pandas as pd
data=[[1,2,3],[4,5,6]]
index=['a','b']#行号
columns=['c','d','e']#列号
df=pd.DataFrame(data,index=index,columns=columns)#生成一个数据框

print df.loc['a':'b']

print df.iloc[0:1]

print df.ix['a':'b']

print df.ix[0:1]
#结果都为
'''
   c  d  e
a  1  2  3
b  4  5  6
'''

4、分别使用loc、iloc、ix 索引多列的数据:

import pandas as pd
data=[[1,2,3],[4,5,6]]
index=['a','b']#行号
columns=['c','d','e']#列号
df=pd.DataFrame(data,index=index,columns=columns)#生成一个数据框

print df.loc[:,'c':'d']

print df.iloc[:,0:2]

print df.ix[:,'c':'d']

print df.ix[:,0:2]
#结果都为
'''
   c  d
a  1  2
b  4  5
'''

 

 

 

1.3 如果想索引列数据,像这样做会报错

import pandas as pd
data = [[1,2,3],[4,5,6]]
index = ['d','e']
columns=['a','b','c']
df = pd.DataFrame(data=data, index=index, columns=columns)
print df.loc['a']
'''
KeyError: 'the label [a] is not in the [index]'
'''
 

1.4 loc可以获取多行数据

import pandas as pd
data = [[1,2,3],[4,5,6]]
index = ['d','e']
columns=['a','b','c']
df = pd.DataFrame(data=data, index=index, columns=columns)
print df.loc['d':]
'''
   a  b  c
d  1  2  3
e  4  5  6
'''
 

1.5 loc扩展——索引某行某列

import pandas as pd
data = [[1,2,3],[4,5,6]]
index = ['d','e']
columns=['a','b','c']
df = pd.DataFrame(data=data, index=index, columns=columns)
print df.loc['d',['b','c']]
'''
b    2
c    3
'''
 

1,6 loc扩展——索引某列

import pandas as pd
data = [[1,2,3],[4,5,6]]
index = ['d','e']
columns=['a','b','c']
df = pd.DataFrame(data=data, index=index, columns=columns)
print df.loc[:,['c']]
'''
   c
d  3
e  6
'''
 

当然获取某列数据最直接的方式是df.[列标签],但是当列标签未知时可以通过这种方式获取列数据。

需要注意的是,dataframe的索引[1:3]是包含1,2,3的,与平时的不同。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

九城风雪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值