Setting the name of the axes in Pandas DataFrame
Last Updated :
23 Jan, 2019
There are multiple operations which can be performed over the exes in Pandas. Let’s see how to operate on row and column index with examples.
For link to the CSV file used in the code, click here.
Reset the name of the row index.
Code #1 : We can reset the name of the DataFrame index by using df.index.name
attribute.
import pandas as pd
df = pd.read_csv( 'nba.csv' )
print (df)
|
Output :
df.index.name = 'Index_name'
print (df)
|
Output :
Code #2 : We can reset the name of the DataFrame index by using df.rename_axis()
function.
import pandas as pd
df = pd.read_csv( 'nba.csv' )
df.rename_axis( 'Index_name' , axis = 'rows' )
print (df)
|
Output :
Reset the name of the column axes
Code #1 : We can reset the name of the DataFrame column axes by using df.rename_axis()
function.
import pandas as pd
df = pd.read_csv( 'nba.csv' )
print (df)
|
Output :
As we can see in the output, column axes of the df DataFrame does not have any name. So, we will set the name using df.rename_axis()
function.
df.rename_axis( 'Column_Index_name' , axis = 'columns' )
print (df)
|
Output :
Share your thoughts in the comments
Please Login to comment...