簡體   English   中英

更改 Pandas dataframe 中索引列的標題

[英]change the title for index column in Pandas dataframe

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

我想將標題“snapshot”更改為“start_of_month”。 我可以應用reset_index() ,更改列標題然后set_index() 但似乎很多矯枉過正。

有沒有更簡單的方法?

正確答案來自@Marat。

df.index.name = 'start_of_month'

你可以使用這個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM