更改熊猫数据框中索引列的标题

时间:2020-05-18 00:43:46

标签: python pandas

import pandas as pd 
import numpy as np

cust_no = np.arange(111,555,111)
snapshot = list(range(201809,201813))
income = np.random.randint(50,5000,32)
expense = np.random.randint(10,1000,32)
df = pd.DataFrame(data = list(zip(cust_no, snapshot,income, expense)), 
                  columns = ['cust_no','snapshot',  'income', 'expense'])

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

我想将标题“快照”更改为“ start_of_month”。我可以应用reset_index(),更改列标题,然后更改set_index()。但是似乎有很多矫kill过正。

有没有更简单的方法?

2 个答案:

答案 0 :(得分:2)

正确答案是@Marat。

df.index.name = 'start_of_month'

答案 1 :(得分:1)

您可以使用此功能:

df.rename(columns = {'snapshot':'start_of_month'}, axis='columns', inplace =True)
print(df.columns)