Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

Pandas index column title or name

mux1 = pd.MultiIndex.from_arrays([['Apples', 'Oranges', 'Puppies', 'Ducks'],
                                  list('abcd')], 
                                  names=['index name 1','index name 1'])


mux2 = pd.MultiIndex.from_product([list('ABC'),
                                  list('XY')], 
                                  names=['col name 1','col name 2'])

df = pd.DataFrame(np.random.randint(10, size=(4,6)), index=mux1, columns=mux2)
print (df)
col name 1                 A     B     C   
col name 2                 X  Y  X  Y  X  Y
index name 1 index name 1                  
Apples       a             2  9  4  7  0  3
Oranges      b             9  0  6  0  9  4
Puppies      c             2  4  6  1  4  4
Ducks        d             6  6  7  1  2  8
#For MultiIndex in index and columns is necessary working with .names instead .name and 
#set by list or tuples: Plural is necessary for check/set values:
print (df.index.name)
None
print (df.columns.name)
None
print (df.index.names)
['index name 1', 'index name 1']
print (df.columns.names)
['col name 1', 'col name 2']

df.rename_axis(('foo','bar'))
df.rename_axis(('baz','bak'), axis=1) #columns
df.rename_axis(index=('foo','bar'), columns=('baz','bak'))
#Removing index and columns names means set it to None:
df2 = df.rename_axis(index=(None,None), columns=(None,None))
#OR
df.index.names = ['foo','bar']
df.columns.names = ['baz','bak']
dir(df.index)
df.index.rename('foo', inplace=True)



Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #Pandas #index #column #title
ADD COMMENT
Topic
Name
5+2 =