由于.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
所以我怎么做,没有链接,显然是为了避免链式分配问题。
答案 0 :(得分:3)
让我们使用.loc和布尔索引,并通过带有索引切片的dataframe列索引访问列标签:
df.loc[df['a'] == 'x',df.columns[-12:-1]] = 3