pandas中df.ix, df.loc, df.iloc 的使用场景以及区别

pandas中df.ix, df.loc, df.iloc 的使用场景以及区别:

https://stackoverflow.com/questions/31593201/pandas-iloc-vs-ix-vs-loc-explanation

# Note: in pandas version 0.20.0 and above, ix is deprecated and the use of loc and iloc is encouraged instead.

# First, a recap:
  ● loc works on labels in the index.
  ● iloc works on the positions in the index (so it only takes integers).
  ● ix usually tries to behave like loc but falls back to behaving like iloc if the label is not in the index.

# Combining position-based and label-based indexing
>>> 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.ix[:'c', :4]
    x   y   z   8
a NaN NaN NaN NaN
b NaN NaN NaN NaN
c 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

# get_loc() is an index method meaning "get the position of the label in this index".
# Note that since slicing with iloc is exclusive of its endpoint, we must add 1 to this value if we want row 'c' as well

转载于:https://www.cnblogs.com/songdanzju/p/7497577.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值