pd.__version__
'0.15.2'
我有一个熊猫的数据框架,有三个层次的多重索引。当我连接这两个数据帧时,它将最低的索引变成了一个应该是字符串的float。
idx=str(dfmaster_stats.index.levels[2]).replace('.0', '')
TypeError: 'FrozenList' does not support mutable operations.
我研究了其他问题,发现多索引是不能更改的,所以我尝试重新索引数据帧。我遵循了这个问题,但两种解决方案都不起作用。你知道吗
Pandas: Modify a particular level of Multiindex
我也试过设置\u级别,但不确定语法。你知道吗
dfmaster_stats.index.set_levels(dfmaster_stats.index.levels[2](idx), level =2)
给我这个错误
TypeError: 'Index' object is not callable
正如在其他文章中提到的,只需重置索引、更改数据类型和设置新索引可能会更容易。你知道吗
输出:
现在,重置索引,更改数据类型,并设置新索引。你知道吗
输出:
一般来说,我发现操纵多索引(index?)有时值得,有时不值得。更改级别会变得冗长。如果你致力于这项事业,这是可行的:
输出:
如果您提供示例代码来创建数据帧,我可以将其扩展到3个级别,或者您可以解决它。:)
相关问题 更多 >
编程相关推荐