loc,iloc, ix区别 Python2.7的DataFrame中的.loc函数和Python3中的区别

Python2.7的DataFrame中的.loc函数和Python3区别

在python2.7中之前用pandas中的DataFrame类型中,.loc表示标签索引, 其实语法没有变,报错的可能是你可能没有搞清楚loc在Series和DataFrame的区别:
loc在Series中直接取标签,loc在DataFrame中要先指定行然后再取标签,因为DataFrame有两个索引,行索引和列索引都要指定。

loc在Series用法

# 构建一个Series
series_obj = pd.Series(range(5), index=['A', 'B', 'C', 'D', 'E'])
print(series_obj)
"""
Series
A    0
B    1
C    2
D    3
E    4
dtype: int64
"""
print(series_obj.loc['A':'D'])
"""
A    0
B    1
C    2
D    3
dtype: int64
"""
print(series_obj.iloc[0:4])
"""
A    0
B    1
C    2
D    3
dtype: int64
"""
print(series_obj.ix[0:4])
"""
A    0
B    1
C    2
D    3
dtype: int64
"""
print(series_obj.ix['A':'D'])
"""
A    0
B    1
C    2
D    3
dtype: int64
"""

loc在DataFrame用法

# 构建DataFrame
df_obj2 = pd.DataFrame(dict_data)

print(df_obj2)
"""
   A          B    C  D       E     F
0  1 2021-04-01  1.0  3  python  test
1  1 2021-04-01  1.0  3    java  test
2  1 2021-04-01  1.0  3       c  test
3  1 2021-04-01  1.0  3  nodejs  test
"""

print(df_obj2.loc[1:3])
"""
   A          B    C  D       E     F
1  1 2021-04-01  1.0  3    java  test
2  1 2021-04-01  1.0  3       c  test
3  1 2021-04-01  1.0  3  nodejs  test
"""

print(df_obj2.loc[1:3, 'A':'D'])
"""
   A          B    C  D
1  1 2021-04-01  1.0  3
2  1 2021-04-01  1.0  3
3  1 2021-04-01  1.0  3
"""

补充 三种索引方式

loc 标签索引
iloc (int loc)位置索引
lx 混合索引 先按默认标签查找 找不到再按位置索引查找

iloc在Series的用法 和loc差不多 只是不取尾

# 构建一个Series
series_obj = pd.Series(range(5), index=['A', 'B', 'C', 'D', 'E'])
print(series_obj)
"""
Series
A    0
B    1
C    2
D    3
E    4
dtype: int64
"""
print(series_obj.iloc[0:4])
"""
A    0
B    1
C    2
D    3
dtype: int64
"""

iloc在DataFrame的用法

print(df_obj2)
"""
   A          B    C  D       E     F
0  1 2021-04-01  1.0  3  python  test
1  1 2021-04-01  1.0  3    java  test
2  1 2021-04-01  1.0  3       c  test
3  1 2021-04-01  1.0  3  nodejs  test
"""

print(df_obj2.iloc[1:3])
"""
   A          B    C  D     E     F
1  1 2021-04-01  1.0  3  java  test
2  1 2021-04-01  1.0  3     c  test
"""

print(df_obj2.iloc[1:3, 2:5])
"""
     C  D     E
1  1.0  3  java
2  1.0  3     c
"""

总结

  1. loc索引取头取尾 iloc取不到尾(标签索引报含头和尾)
  2. iloc不指定行和列,取所有行和列
  3. ix标签索引和位置索引都可以取到,一般先按标签索引取,再按位置索引

ix在Series用法:

# 构建一个Series
series_obj = pd.Series(range(5), index=['A', 'B', 'C', 'D', 'E'])
print(series_obj)
"""
Series
A    0
B    1
C    2
D    3
E    4
dtype: int64
"""
print(series_obj.ix[0:4])
"""
A    0
B    1
C    2
D    3
dtype: int64
"""
print(series_obj.ix['A':'D'])
"""
A    0
B    1
C    2
D    3
dtype: int64
"""

ix在DataFrame用法:

print(df_obj2)
"""
   A          B    C  D       E     F
0  1 2021-04-01  1.0  3  python  test
1  1 2021-04-01  1.0  3    java  test
2  1 2021-04-01  1.0  3       c  test
3  1 2021-04-01  1.0  3  nodejs  test
"""

#  ix先把0当作标签索引去找 在不到再按照位置索引去找
print(df_obj2.ix[0:4, 0])
"""
0    1
1    1
2    1
3    1
"""

print(df_obj2.ix[0:4, 'A':'D'])
"""
   A          B    C  D
0  1 2021-04-01  1.0  3
1  1 2021-04-01  1.0  3
2  1 2021-04-01  1.0  3
3  1 2021-04-01  1.0  3
"""

print(df_obj2.ix[0:4, 0:4])
"""
   A          B    C  D
0  1 2021-04-01  1.0  3
1  1 2021-04-01  1.0  3
2  1 2021-04-01  1.0  3
3  1 2021-04-01  1.0  3
"""
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

BRYTLEVSON

打赏的都是天使,创作的动力

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

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

打赏作者

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

抵扣说明:

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

余额充值