Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Labeling indexes on a dataframe

I have a multi-layer index in a dataframe. When I run

print(len(b.index.names))

I get 3. When I run

print(b.index.names)

I get [None, None, None].

How do I give each of the above index levels a unique name?

like image 452
thomassantosh Avatar asked Jun 05 '26 11:06

thomassantosh


2 Answers

Either

b.rename_axis(['X', 'Y', 'Z'])

Or

b.index.names = ['X', 'Y', 'Z']
like image 198
piRSquared Avatar answered Jun 08 '26 02:06

piRSquared


You can also assign with list such that indexes are named index_1, index_2, and index_3 respectively, if more they are named accordingly as well:

b.index.names = ["index_" + str(i+1) for i in range(len(b.index.names))]
like image 37
student Avatar answered Jun 08 '26 02:06

student