Reputation: 33
I have just started leaning pandas and this is really my first question here so pls don't mind if its too basic !
When to use df.index.name
and when df.index.names
?
Would really appreciate to know the difference and application.
Many Thanks
Upvotes: 3
Views: 962
Reputation: 30589
name
returns the name of the Index or MultiIndex.
names
returns a list of the level names of an Index (just one level) or a MultiIndex (more than one level).
Index:
df1 = pd.DataFrame(columns=['a', 'b', 'c']).set_index('a')
print(df1.index.name, df1.index.names)
# a ['a']
MultiIndex:
df2 = pd.DataFrame(columns=['a', 'b', 'c']).set_index(['a', 'b'])
print(df2.index.name, df2.index.names)
# None ['a', 'b']
df2.index.name = 'my_multiindex'
print(df2.index.name, df2.index.names)
# my_multiindex ['a', 'b']
Upvotes: 4