How to get rows/index names in Pandas dataframe
Last Updated :
29 Sep, 2023
While analyzing the real datasets which are often very huge in size, we might need to get the rows or index names in order to perform some certain operations. Let's discuss how to get row names in Pandas
dataframe. First, let's create a simple dataframe with
nba.csv
Python3
# Import pandas package
import pandas as pd
# making data frame
data = pd.read_csv("https://media.geeksforgeeks.org/wp-content/uploads/nba.csv")
# calling head() method
# storing in new variable
data_top = data.head(10)
# display
data_top

Now let's try to get the row name from above dataset.
Method #1:
Simply iterate over indices
Python3
# Import pandas package
import pandas as pd
# making data frame
data = pd.read_csv("nba.csv")
# calling head() method
# storing in new variable
data_top = data.head()
# iterating the columns
for row in data_top.index:
print(row, end = " ")
Output:
0 1 2 3 4 5 6 7 8 9
Method #2:
Using rows with dataframe object
Python3
# Import pandas package
import pandas as pd
# making data frame
data = pd.read_csv("nba.csv")
# calling head() method
# storing in new variable
data_top = data.head()
# list(data_top) or
list(data_top.index)
Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Method #3:
index.values
method returns an array of index.
Python3
# Import pandas package
import pandas as pd
# making data frame
data = pd.read_csv("nba.csv")
# calling head() method
# storing in new variable
data_top = data.head()
list(data_top.index.values)
Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Method #4:
Using
tolist()
method with values with given the list of index.
Python3
# Import pandas package
import pandas as pd
# making data frame
data = pd.read_csv("nba.csv")
# calling head() method
# storing in new variable
data_top = data.head()
list(data_top.index.values.tolist())
Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Method #5:
Count number of rows in dataframe Since we have loaded only 10 top rows of dataframe using
head()
method, let's verify total number of rows first.
Python3
# iterate the indices and print each one
for row in data.index:
print(row, end= " ")
Output:

Now, let's print the total count of index.
Python3
# Import pandas package
import pandas as pd
# making data frame
data = pd.read_csv("nba.csv")
row_count = 0
# iterating over indices
for col in data.index:
row_count += 1
# print the row count
print(row_count)
Output:
458
Similar Reads
How to get nth row in a Pandas DataFrame? Pandas Dataframes are basically table format data that comprises rows and columns. Now for accessing the rows from large datasets, we have different methods like iloc, loc and values in Pandas. The most commonly used method is iloc(). Let us consider a simple example.Method 1. Using iloc() to access
4 min read
How to Get Column Names in Pandas Dataframe While analyzing the real datasets which are often very huge in size, we might need to get the pandas column names in order to perform certain operations. The simplest way to get column names in Pandas is by using the .columns attribute of a DataFrame. Let's understand with a quick example:Pythonimpo
4 min read
How to Get First Row of Pandas DataFrame? To get the first row of a Pandas Dataframe there are several methods available, each with its own advantages depending on the situation. The most common methods include using .iloc[], .head(), and .loc[]. Let's understand with this example:Pythonimport pandas as pd data = {'Name': ['Alice', 'Bob', '
4 min read
How to get column and row names in DataFrame? While analyzing the real datasets which are often very huge in size, we might need to get the rows or index names and columns names in order to perform certain operations. Note: For downloading the nba dataset used in the below examples Click Here Getting row names in Pandas dataframe First, let's
3 min read
Convert a column to row name/index in Pandas Pandas provide a convenient way to handle data and its transformation. Let's see how can we convert a column to row name/index in Pandas. Create a dataframe first with dict of lists. Python3 # importing pandas as pd import pandas as pd # Creating a dict of lists data = {'Name':["Akash", "Geeku", "
2 min read
How to Sort a Pandas DataFrame based on column names or row index? Pandas dataframe.sort_index() method sorts objects by labels along the given axis. Basically, the sorting algorithm is applied to the axis labels rather than the actual data in the Dataframe and based on that the data is rearranged. Creating Pandas Dataframe Create a DataFrame object from the Python
3 min read
How to Convert Index to Column in Pandas Dataframe? Pandas is a powerful tool which is used for data analysis and is built on top of the python library. The Pandas library enables users to create and manipulate dataframes (Tables of data) and time series effectively and efficiently. These dataframes can be used for training and testing machine learni
2 min read
Change column names and row indexes in Pandas DataFrame Given a Pandas DataFrame, let's see how to change its column names and row indexes. About Pandas DataFramePandas DataFrame are rectangular grids which are used to store data. It is easy to visualize and work with data when stored in dataFrame. It consists of rows and columns.Each row is a measuremen
4 min read
How to get name of dataframe column in PySpark ? In this article, we will discuss how to get the name of the Dataframe column in PySpark. To get the name of the columns present in the Dataframe we are using the columns function through this function we will get the list of all the column names present in the Dataframe. Syntax: df.columns We can a
3 min read
How to print Dataframe in Python without Index? When printing a Dataframe, by default, the index appears with the output but this can be removed if required. we will explain how to print pandas DataFrame without index with different methods. Creating Pandas DataFrame without Index Python3 import pandas as pd df = pd.DataFrame({"Name": ["sachin",
1 min read