Pandas Index Column Title or Name

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



Leave a reply



Submit