首页 > 数据分析

详解pandas中iloc, loc和ix的区别和联系

时间:2020-08-05 数据分析 查看: 1345

Pandas库十分强大,但是对于切片操作iloc, loc和ix,很多人对此十分迷惑,因此本篇博客利用例子来说明这3者之一的区别和联系,尤其是iloc和loc。

对于ix,由于其操作有些复杂。

首先,介绍这三种方法的概述:

  • loc gets rows (or columns) with particular labels from the index. loc从索引中获取具有特定标签的行(或列)。这里的关键是:标签。标签的理解就是name名字。
  • iloc gets rows (or columns) at particular positions in the index (so it only takes integers). iloc在索引中的特定位置获取行(或列)(因此它只接受整数)。这里的关键是:位置。位置的理解就是排第几个。
  • ix usually tries to behave like loc but falls back to behaving like iloc if a label is not present in the index. ix通常会尝试像loc一样行为,但如果索引中不存在标签,则会退回到像iloc一样的行为。

接下来,举几个例子说明:

1 loc

其实,对于loc始终坚持一个原则:loc是基于label进行索引的!

import pandas as pd
df1 = pd.DataFrame(data= [[1, 2, 3],[4, 5, 6], [7, 8, 9]], index=[0, 1, 2], columns=['a','b','c'])
df2 = pd.DataFrame(data= [[1, 2, 3],[4, 5, 6], [7, 8, 9]], index=['e', 'f', 'g'], columns=['a','b','c'])
print(df1)
print(df2)
'''
df1:
  a b c
0 1 2 3
1 4 5 6
2 7 8 9
df2:
  a b c
e 1 2 3
f 4 5 6
g 7 8 9
'''

# loc索引行,label是整型数字
print(df1.loc[0])
'''
a  1
b  2
c  3
Name: 0, dtype: int64
'''

# loc索引行,label是字符型
print(df2.loc['e'])
'''
a  1
b  2
c  3
Name: 0, dtype: int64
'''
# 如果对df2这么写:df2.loc[0]会报错,因为loc索引的是label,显然在df2的行的名字中没有叫0的。
print(df2.loc[0])
'''
TypeError: cannot do slice indexing on <class 'pandas.core.indexes.base.Index'> with these indexers [0] of <class 'int'>
'''

# loc索引多行数据
print(df1.loc[1:])
'''
  a b c
1 4 5 6
2 7 8 9
'''

# loc索引多列数据
print(df1.loc[:,['a', 'b']])
'''
  a b
0 1 2
1 4 5
2 7 8
'''
# df1.loc[:,0:2]这么写报错, 因为loc索引的是label,显然在df1的列的名字中没有叫0,1和2的。
print(df1.loc[:,0:2])
'''
TypeError: cannot do slice indexing on <class 'pandas.core.indexes.base.Index'> with these indexers [0] of <class 'int'>
'''

# locs索引某些行某些列
print(df1.loc[0:2, ['a', 'b']])
'''
  a b
0 1 2
1 4 5
2 7 8
'''

2 iloc

其实,对于iloc始终坚持一个原则:iloc是基于position进行索引的!

import pandas as pd
df1 = pd.DataFrame(data= [[1, 2, 3],[4, 5, 6], [7, 8, 9]], index=[0, 1, 2], columns=['a','b','c'])
df2 = pd.DataFrame(data= [[1, 2, 3],[4, 5, 6], [7, 8, 9]], index=['e', 'f', 'g'], columns=['a','b','c'])
print(df1)
print(df2)
'''
df1:
  a b c
0 1 2 3
1 4 5 6
2 7 8 9
df2:
  a b c
e 1 2 3
f 4 5 6
g 7 8 9
'''
# iloc索引行,label是整型数字
print(df1.iloc[0])
'''
a  1
b  2
c  3
Name: 0, dtype: int64
'''

# iloc索引行,label是字符型。如果按照loc的写法来写应该是:df2.iloc['e'],显然这样报错,因为iloc不认识label,它是基于位置的。
print(df2.iloc['e'])
'''
TypeError: cannot do positional indexing on <class 'pandas.core.indexes.base.Index'> with these indexers [e] of <class 'str'>
'''
# iloc索引行,label是字符型。正确的写法应该如下:
# 也就说,不论index是什么类型的,iloc只能写位置,也就是整型数字。
print(df2.iloc[0])
'''
a  1
b  2
c  3
Name: e, dtype: int64
'''

# iloc索引多行数据
print(df1.iloc[1:])
'''
  a b c
1 4 5 6
2 7 8 9
'''

# iloc索引多列数据
# 如果如下写法,报错。
print(df1.iloc[:,['a', 'b']])
'''
TypeError: cannot perform reduce with flexible type
'''
# iloc索引多列数据, 正确写法如下:
print(df1.iloc[:,0:2])
'''
  a b
0 1 2
1 4 5
2 7 8
'''

# iloc索引某些行某些列
print(df1.iloc[0:2, 0:1])
'''
  a
0 1
1 4
'''

3 ix

ix的操作比较复杂,在pandas版本0.20.0及其以后版本中,ix已经不被推荐使用,建议采用iloc和loc实现ix。

到此这篇关于详解pandas中iloc, loc和ix的区别和联系的文章就介绍到这了,更多相关pandas iloc loc ix内容请搜索python博客以前的文章或继续浏览下面的相关文章希望大家以后多多支持python博客!

展开全文
上一篇:Python unittest 自动识别并执行测试用例方式
下一篇:PyCharm 无法 import pandas 程序卡住的解决方式
输入字:
相关知识
python数据挖掘使用Evidently创建机器学习模型仪表板

在本文中,我们将探索 Evidently 并创建交互式报告/仪表板。有需要的朋友欢迎大家收藏学习,希望能够有所帮助,祝大家多多进步早日升职加薪

Python多进程共享numpy 数组的方法

本文章主要介绍了Python多进程共享numpy 数组的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

python数据分析近年比特币价格涨幅趋势分布

这篇文章主要为大家介绍了python分析近年来比特币价格涨幅趋势的数据分布,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步

python调用matlab的方法详解

这篇文章主要为大家介绍了python调用matlab,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助