Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Pandas 熊猫中多索引的重命名_Pandas_Dataframe_Indexing - Fatal编程技术网

Pandas 熊猫中多索引的重命名

Pandas 熊猫中多索引的重命名,pandas,dataframe,indexing,Pandas,Dataframe,Indexing,有没有办法在多索引数据框中重命名索引 范例 import pandas as pd df = pd.DataFrame([ ['green','M', 10.1, 'class2'], ['red', 'L', 13.5, 'class1']]) df.columns = ['color', 'size', 'price', 'classifier'] df.set_index(['color','size'], inplace=True) 给予 我想将索引“size”重命名为size\

有没有办法在多索引数据框中重命名索引

范例

import pandas as pd


df = pd.DataFrame([
['green','M', 10.1, 'class2'],
['red', 'L', 13.5, 'class1']])
df.columns = ['color', 'size', 'price', 'classifier']

df.set_index(['color','size'], inplace=True)
给予

我想将索引“size”重命名为size\u modified

df.rename(index={'size': 'Size_Mod'}, inplace=True) 
不起作用

您需要而不是
df。重命名

df = df.rename_axis(index={'size': 'Size_Mod'}) 


另一种方法是,确保名称的顺序正确。但它的指纹更小

df.index.names = ['color','Size_Mod']
print(df)

                price classifier
color Size_Mod                  
green M          10.1     class2
red   L          13.5     class1
df.index.names = ['color','Size_Mod']