大熊猫索引标题与列标题一致

Ari*_*iel 5 python dataframe pandas

对于我的数据集,我想用数据帧中的第一列替换自动索引,并用数据帧的所有列名内联设置新的索引标题。

原始数据集:

import numpy as np
import pandas as pd
df = pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6]]),columns=['a','b','c'])
df

  a b c 
0 1 2 3
1 4 5 6
Run Code Online (Sandbox Code Playgroud)

将索引设置为第一列:

df.set_index('a',inplace=True)
df

  b c
a 
1 2 3
4 5 6
Run Code Online (Sandbox Code Playgroud)

但是,现在我仍然停留在如何使索引标题与列标题内联的问题上。以下是我正在寻找的输出:

a b c
1 2 3
4 5 6
Run Code Online (Sandbox Code Playgroud)

jez*_*ael 6

Use this trick, but I think it is not best practice:

df.columns.name = df.index.name
df.index.name = None
print (df)
a  b  c
1  2  3
4  5  6
Run Code Online (Sandbox Code Playgroud)