thomassantosh
thomassantosh

Reputation: 607

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?

Upvotes: 3

Views: 50

Answers (2)

niraj
niraj

Reputation: 18208

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))]

Upvotes: 1

piRSquared
piRSquared

Reputation: 294218

Either

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

Or

b.index.names = ['X', 'Y', 'Z']

Upvotes: 1

Related Questions