Pandas index column title or name
You can just get/set the index via its name
property
In [7]: df.index.name
Out[7]: 'Index Title'
In [8]: df.index.name = 'foo'
In [9]: df.index.name
Out[9]: 'foo'
In [10]: df
Out[10]:
Column 1
foo
Apples 1
Oranges 2
Puppies 3
Ducks 4
change the title for index column in Pandas dataframe
The correct answer was by @Marat.
df.index.name = 'start_of_month'
Set index name of pandas DataFrame
if ingredients is the name of the index then you can set it by
df.index.name='ingredient'
With the current solutions you have 'ingredient' as name of the index, which is printed in different row to that of column names. This cannot be changed as is. Try the modified solution below, here the index is copied on to a new column with column name and the index replaced with sequence of numbers.
df['ingredient']=df.index
df = df.reset_index(drop=True)
Rename Pandas DataFrame Index
The rename
method takes a dictionary for the index which applies to index values.
You want to rename to index level's name:
df.index.names = ['Date']
A good way to think about this is that columns and index are the same type of object (Index
or MultiIndex
), and you can interchange the two via transpose.
This is a little bit confusing since the index names have a similar meaning to columns, so here are some more examples:
In [1]: df = pd.DataFrame([[1, 2, 3], [4, 5 ,6]], columns=list('ABC'))
In [2]: df
Out[2]:
A B C
0 1 2 3
1 4 5 6
In [3]: df1 = df.set_index('A')
In [4]: df1
Out[4]:
B C
A
1 2 3
4 5 6
You can see the rename on the index, which can change the value 1:
In [5]: df1.rename(index={1: 'a'})
Out[5]:
B C
A
a 2 3
4 5 6
In [6]: df1.rename(columns={'B': 'BB'})
Out[6]:
BB C
A
1 2 3
4 5 6
Whilst renaming the level names:
In [7]: df1.index.names = ['index']
df1.columns.names = ['column']
Note: this attribute is just a list, and you could do the renaming as a list comprehension/map.
In [8]: df1
Out[8]:
column B C
index
1 2 3
4 5 6
Title to index column doesn't remain - Python Pandas DataFrame
After the df.index.rename('indexTitle', inplace=True)
line I added this:df.to_csv('test_set2.csv', encoding='utf-8')
which updated the file I had.
Related Topics
Configuring So That Pip Install Can Work from Github
How to Insert Pandas Dataframe via MySQLdb into Database
Pandas Create New Column with Count from Groupby
Pycharm Current Working Directory
Syntaxerror: Multiple Statements Found While Compiling a Single Statement
How Many Concurrent Requests Does a Single Flask Process Receive
Import Multiple Excel Files into Python Pandas and Concatenate Them into One Dataframe
Override Python's 'In' Operator
Plotting Grouped Data in Same Plot Using Pandas
Super() Raises "Typeerror: Must Be Type, Not Classobj" for New-Style Class
Python How to Pad Numpy Array with Zeros
Why 'Torch.Cuda.Is_Available()' Returns False Even After Installing Pytorch with Cuda
What Does the Term "Broadcasting" Mean in Pandas Documentation
Change the Colors Within Certain Range to Another Color Using Opencv
Differencebetween Using Loc and Using Just Square Brackets to Filter for Columns in Pandas/Python