EdC*_*ica 35
您可以rename从reset_index以下位置调用返回的df :
In [145]:
# create a df
df = pd.DataFrame(np.random.randn(5,3))
df
Out[145]:
0 1 2
0 -2.845811 -0.182439 -0.526785
1 -0.112547 0.661461 0.558452
2 0.587060 -1.232262 -0.997973
3 -1.009378 -0.062442 0.125875
4 -1.129376 3.282447 -0.403731
Run Code Online (Sandbox Code Playgroud)
设置索引名称
In [146]:
df.index = df.index.set_names(['foo'])
df
Out[146]:
0 1 2
foo
0 -2.845811 -0.182439 -0.526785
1 -0.112547 0.661461 0.558452
2 0.587060 -1.232262 -0.997973
3 -1.009378 -0.062442 0.125875
4 -1.129376 3.282447 -0.403731
Run Code Online (Sandbox Code Playgroud)
电话reset_index和连锁rename:
In [147]:
df.reset_index().rename(columns={df.index.name:'bar'})
Out[147]:
bar 0 1 2
0 0 -2.845811 -0.182439 -0.526785
1 1 -0.112547 0.661461 0.558452
2 2 0.587060 -1.232262 -0.997973
3 3 -1.009378 -0.062442 0.125875
4 4 -1.129376 3.282447 -0.403731
Run Code Online (Sandbox Code Playgroud)
感谢@ayhan
或者你可以rename_axis用来重命名索引reset_index:
In [149]:
df.rename_axis('bar').reset_index()
Out[149]:
bar 0 1 2
0 0 -2.845811 -0.182439 -0.526785
1 1 -0.112547 0.661461 0.558452
2 2 0.587060 -1.232262 -0.997973
3 3 -1.009378 -0.062442 0.125875
4 4 -1.129376 3.282447 -0.403731
Run Code Online (Sandbox Code Playgroud)
或者直接首先直接覆盖索引名称:
df.index.name = 'bar'
Run Code Online (Sandbox Code Playgroud)
然后打电话 reset_index
igo*_*rkf 26
您可以这样做(2020 年 1 月):
df = df.reset_index().rename(columns={'index': 'bar'})
print(df)
bar 0 1 2
0 0 -2.845811 -0.182439 -0.526785
1 1 -0.112547 0.661461 0.558452
2 2 0.587060 -1.232262 -0.997973
3 3 -1.009378 -0.062442 0.125875
4 4 -1.129376 3.282447 -0.403731
Run Code Online (Sandbox Code Playgroud)
您可以尝试1.5.0 版本names中引入的参数,来自:DataFrame.reset_index()
名称:int、str 或一维列表,默认 None
使用给定的字符串,重命名包含索引数据的 DataFrame 列。如果 DataFrame 具有 MultiIndex,则它必须是长度等于级别数的列表或元组。
对于单个索引级别数据帧:
class max_speed name class max_speed
falcon bird 389.0 df.reset_index(names='name') 0 falcon bird 389.0
parrot bird 24.0 ------------------------------> 1 parrot bird 24.0
lion mammal 80.5 2 lion mammal 80.5
monkey mammal NaN 3 monkey mammal NaN
Run Code Online (Sandbox Code Playgroud)
对于多索引级别数据帧:
speed species classes names speed species
max type max type
class name 0 bird falcon 389.0 fly
bird falcon 389.0 fly df.reset_index(names=['classes', 'names']) 1 bird parrot 24.0 fly
parrot 24.0 fly --------------------------------------------> 2 mammal lion 80.5 run
mammal lion 80.5 run 3 mammal monkey NaN jump
monkey NaN jump
Run Code Online (Sandbox Code Playgroud)
如果您只关心 Multiindex 中的一级
speed species classes speed species
max type max type
class name df.reset_index(level='class', names='classes') name
bird falcon 389.0 fly ------------------------------------------------> falcon bird 389.0 fly
parrot 24.0 fly parrot bird 24.0 fly
mammal lion 80.5 run lion mammal 80.5 run
monkey NaN jump monkey mammal NaN jump
Run Code Online (Sandbox Code Playgroud)
注意:reset_index()默认情况下不放置,您需要将结果分配回或使用inplace=True,例如
class max_speed name class max_speed
falcon bird 389.0 df.reset_index(names='name') 0 falcon bird 389.0
parrot bird 24.0 ------------------------------> 1 parrot bird 24.0
lion mammal 80.5 2 lion mammal 80.5
monkey mammal NaN 3 monkey mammal NaN
Run Code Online (Sandbox Code Playgroud)
For a Series you can specify the name directly. E.g.:
>>> df.groupby('s1').size().reset_index(name='new_name')
s1 new_name
0 b 1
1 r 1
2 s 1
Run Code Online (Sandbox Code Playgroud)