自从Pandas 0.20.ix已被弃用以来,我想知道在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
那么我怎么做,没有链接,显然是为了避免链式分配问题.
让我们将 .loc 与布尔索引一起使用,并通过带有索引切片的数据框列索引访问列标签:
df.loc[df['a'] == 'x', df.columns[-12:-1]] = 3
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1549 次 |
| 最近记录: |