The DataFrame.index
attribute in Pandas returns the index(row labels) of a given DataFrame.
The syntax for the DataFrame.index
attribute is given below:
DataFrame.index attribute
Since it is an attribute, DataFrame.index
does not take any parameter value.
The DataFrame.index
attribute returns the index row labels of a DataFrame.
# A code to illustrate the DataFrame.index attribute# importing the pandas moduleimport pandas as pd# creating a dataframedf = pd.DataFrame({'Nme': {0: 'Alek', 1: 'Akim', 2: 'Cynthia'},'Age': {0: 20, 1: 30, 2: 50},'Sex': {0: "Male", 1: "Male", 2: "Female"}})# printing the dataframeprint(df)# obtaining the index labels of the DataFrameindex = df.indexprint(index)
pandas
library.df
using the pandas.DataFrame()
function.df
.df
using the DataFrame.index
attribute. We assign the result to a variable, index
.index
variable.