大熊猫混合位置和标签索引没有链接

时间:2017-06-26 16:12:27

标签: pandas slice

由于.ix已经deprecated as of Pandas 0.20,我想知道在Pandas中混合基于标签,基于布尔和基于位置的索引的正确方法是什么?我需要为一个数据帧切片赋值,这些数据帧可以在索引和列上的位置上使用label或boolean进行最佳引用。例如(使用.loc作为所需切片方法的占位符):

df.loc[df['a'] == 'x', -12:-1] = 3

显然这不起作用,我得到了:

TypeError: cannot do slice indexing on <class 'pandas.core.indexes.base.Index'> with these indexers [-12] of <class 'int'>

如果我使用.iloc,我会:

NotImplementedError: iLocation based boolean indexing on an integer type is not available

所以我怎么做,没有链接,显然是为了避免链式分配问题。

1 个答案:

答案 0 :(得分:3)

让我们使用.loc和布尔索引,并通过带有索引切片的dataframe列索引访问列标签:

df.loc[df['a'] == 'x',df.columns[-12:-1]] = 3