Search
 
SCRIPT & CODE EXAMPLE
 

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)



Comment

PREVIOUS NEXT
Code Example
Python :: plotly two y axis bar chart grouped 
Python :: ascci value pyth 
Python :: django q and f 
Python :: To do floor division and get an integer result (discarding any fractional result) 
Python :: scraped text in Russian encoding python 
Python :: lists example in python 
Python :: Python send sms curl 
Python :: iterate over the dataset and compute the mean vector. 
Python :: how to hide a specific file in python 
Python :: queue data structure in python 
Python :: NLP text summarization with LSA 
Python :: dynamic id python 
Python :: get list values in b/w indexes python 
Python :: ValueError: y_true and y_pred contain different number of classes 6, 2. Please provide the true labels explicitly through the labels argument. Classes found in y_true: [0 1 2 3 4 5] 
Python :: csv/gpd to shapefile python 
Python :: how to truncate a float in jinja template 
Python :: append to multidimensional list python 
Python :: how to check if the update_one success in flask 
Python :: buscar elemento en lista python 
Python :: how to add other categories in django admin template 
Python :: Combine first and last 3 columns into new dataframe 
Python :: basic decorator example 
Python :: visualizing of convolutional kernels using pytorch 
Python :: python arcade sound 
Python :: create a python file and import it as library in other file 
Python :: site:github.com python ssh 
Python :: python continue inner for loop 
Python :: dashbars detect first loop 
Python :: not en python 
Python :: What are zinc bandages used for? 
ADD CONTENT
Topic
Content
Source link
Name
3+1 =