FutureWarning: .ix is deprecated. Please use .loc for label based indexing or .iloc for posi

58 篇文章 0 订阅
16 篇文章 0 订阅

IX索引器已弃用
警告 在0.20.0开始,.ix索引器已被弃用,赞成更加严格.iloc 和.loc索引。

.ix在推断用户想要做什么方面提供了很多魔力。也就是说,.ix可以根据索引的数据类型决定通过标签进行位置或索引。多年来,这引起了相当多的用户混乱。

推荐的索引编制方法是:

.loc如果要标记索引。

.iloc如果要位置索引。

In [93]: dfd = pd.DataFrame({'A': [1, 2, 3],
   ....:                     'B': [4, 5, 6]},
   ....:                    index=list('abc'))

…:

In [94]: dfd
Out[94]: 
   A  B
a  1  4
b  2  5
c  3  6

先前的行为,您希望从“ A”列的索引中获取第0个元素和第2个元素。

In [3]: dfd.ix[[0, 2], 'A']
Out[3]:
a    1
c    3

Name: A, dtype: int64
使用.loc。在这里,我们将从索引中选择适当的索引,然后使用标签索引。

In [95]: dfd.loc[dfd.index[[0, 2]], 'A']
Out[95]: 
a    1
c    3
Name: A, dtype: int64

也可以使用来表达这一点.iloc,方法是明确获取索引器上的位置,并使用 位置索引来选择事物。

In [96]: dfd.iloc[[0, 2], dfd.columns.get_loc('A')]
Out[96]: 
a    1
c    3
Name: A, dtype: int64

要获取多个索引器,请使用.get_indexer:

In [97]: dfd.iloc[[0, 2], dfd.columns.get_indexer(['A', 'B'])]
Out[97]: 
   A  B
a  1  4
c  3  6

不建议使用缺少标签的列表建立索引
警告 从0.21.0开始,不推荐使用.loc或[]带有一个或多个缺少标签的列表,而推荐使用.reindex。
在以前的版本中,.loc[list-of-labels]只要找到至少一个键,使用就可以工作(否则会引发KeyError)。不建议使用此行为,它将显示一条警告消息,指向此部分。推荐的替代方法是使用.reindex()。

例如。

In [98]: s = pd.Series([1, 2, 3])

In [99]: s
Out[99]: 
0    1
1    2
2    3
dtype: int64

找到所有键的选择保持不变。

In [100]: s.loc[[1, 2]]
Out[100]: 
1    2
2    3
dtype: int64

先前的行为

In [4]: s.loc[[1, 2, 3]]
Out[4]:
1    2.0
2    3.0
3    NaN
dtype: float64

当前行为

In [4]: s.loc[[1, 2, 3]]
Passing list-likes to .loc with any non-matching elements will raise
KeyError in the future, you can use .reindex() as an alternative.

See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/indexing.html#deprecate-loc-reindex-listlike

Out[4]:
1    2.0
2    3.0
3    NaN
dtype: float64

重建索引
实现选择可能找不到的元素的惯用方式是通过.reindex()。另请参阅“ 重新索引编制 ”部分。

In [101]: s.reindex([1, 2, 3])
Out[101]: 
1    2.0
2    3.0
3    NaN
dtype: float64

另外,如果您只想选择有效的密钥,则以下是惯用且有效的;保证保留选择的dtype。

In [102]: labels = [1, 2, 3]

In [103]: s.loc[s.index.intersection(labels)]
Out[103]: 
1    2
2    3
dtype: int64
索引重复会产生.reindex():

In [104]: s = pd.Series(np.arange(4), index=['a', 'a', 'b', 'c'])

In [105]: labels = ['c', 'd']
In [17]: s.reindex(labels)
ValueError: cannot reindex from a duplicate axis

通常,您可以将所需标签与当前轴相交,然后重新索引。

In [106]: s.loc[s.index.intersection(labels)].reindex(labels)
Out[106]: 
c    3.0
d    NaN
dtype: float64

但是,如果您生成的索引重复,这仍然会增加。

In [41]: labels = ['a', 'd']

In [42]: s.loc[s.index.intersection(labels)].reindex(labels)
ValueError: cannot reindex from a duplicate axis

原文:
https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#ix-indexer-is-deprecated

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值