【发布时间】:2021-03-15 21:49:24
【问题描述】:
在 pandas 表中,row-index 和 column-index 具有非常相似的接口,并且一些操作允许通过参数axis 沿着任一行和列进行操作。 (例如sort_index 等等。)
但是如何通过指定轴来访问(读取和写入)行索引或列索引?
# Instead of this
if axis==0:
table.index = some_function(table.get_index_by_axis(axis))
else:
table.column = some_function(table.get_index_by_axis(axis))
# I would like to simply write:
newIndex = some_function(table.get_index_by_axis(axis))
table.set_index_by_axis(newIndex, axis=axis)
是否存在 get_index_by_axis 和 set_index_by_axis 之类的东西?
更新:
数据框有一个属性axes,允许按索引选择轴。但是,这是只读的。分配新值不会对表产生影响。
index = table.axes[axis] # Read an index
newIndex = some_function(index)
table.axes[axis] = newIndex # This has no effect on table.
【问题讨论】: