<分区>
我有一个名为 df 的 Pandas 数据框:
1 2 3 4 5 6 7 8 9 10
dog jumps 1 1 1 1 1 1 0 1 1 1
fox 1 1 1 1 1 1 0 0 1 1
the 1 1 1 1 1 1 1 0 1 1
dog 1 1 1 1 1 1 0 0 1 1
over 1 1 1 1 1 1 0 0 1 1
fox jumps 1 1 1 1 1 1 0 1 1 1
fox 1 1 1 1 1 1 0 0 1 1
the 1 1 1 1 1 1 1 0 1 1
dog 1 1 1 1 1 1 0 0 1 1
over 1 1 1 1 1 1 0 0 1 1
jumps jumps 1 1 1 1 1 1 0 0 1 0
fox 1 1 1 1 1 1 1 0 1 0
the 1 0 1 1 1 1 0 0 1 0
dog 1 1 1 1 1 1 1 0 1 0
over 1 0 1 1 1 0 0 1 1 0
over jumps 1 1 0 1 0 1 1 0 1 0
fox 1 1 1 1 1 1 0 0 1 0
the 1 0 1 1 1 0 0 1 1 0
dog 1 1 1 1 1 1 0 0 1 0
over 1 1 1 1 1 1 0 0 1 0
the jumps 1 1 0 1 1 1 0 0 1 0
fox 1 1 1 1 1 1 0 1 1 0
the 1 1 1 1 1 1 0 0 1 0
dog 1 1 1 1 1 1 0 1 1 0
over 1 1 0 1 0 1 1 0 1 0
我有以下字典:
dic = {'dog': 1, 'fox': 1, 'the': 2, 'over': 2, 'jumps': 0}
我想用 dic 中的相应值替换索引的值。
我尝试了以下方法:
df.index.levels[0][0] = 'integer value'
但这不起作用,它给了我错误:
TypeError: Index does not support mutable operations
还有 pandas rename function也不行
关于如何以最有效和干净的方式执行此操作的任何想法。任何建议将不胜感激。