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

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

I want to change the title "snapshot" to "start_of_month". I could apply reset_index(), change column title and then set_index(). But seems like a lot of overkill.

有没有更简单的方法?

评论
22相亲
22相亲

您可以使用此:

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