The Wayback Machine - https://web.archive.org/web/20240910201622/https://www.geeksforgeeks.org/how-to-get-rows-index-names-in-pandas-dataframe/
Open In App

How to get rows/index names in Pandas dataframe

Last Updated : 29 Sep, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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
   
# 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

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 measurement of some instance while column is a vector which
4 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 list of tuples with columns and indices, say colu
3 min read
Add column names to dataframe in Pandas
In this article, we are going to see how to add column names to a dataframe. Let us how to add names to DataFrame columns in Pandas. Add Column Names to Pandas DataframeBelow are the steps and methods by which we can add column names in the Pandas dataframe in Python: Creating the DataFrameLet's first create an example DataFrame for demonstration r
2 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 create a simple dataframe with nba.csv C/C++ Code #
3 min read
How to Get Column Names in Pandas
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 some certain operations. Let's discuss how to get column names in the Pandas . Syntax to Get Column Names in Pandas # Get column names as an Index objectcolumn_names = df.columns Get Column Names in Pandas Get a list
3 min read
Difference Between Spark DataFrame and Pandas DataFrame
Dataframe represents a table of data with rows and columns, Dataframe concepts never change in any Programming language, however, Spark Dataframe and Pandas Dataframe are quite different. In this article, we are going to see the difference between Spark dataframe and Pandas Dataframe. Pandas DataFrame Pandas is an open-source Python library based o
3 min read
Pandas Dataframe.to_numpy() - Convert dataframe to Numpy array
Pandas DataFrame is a two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). This data structure can be converted to NumPy ndarray with the help of the DataFrame.to_numpy() method. In this article we will see how to convert dataframe to numpy array. Syntax of Pandas DataFrame.to_numpy()
3 min read
Convert given Pandas series into a dataframe with its index as another column on the dataframe
First of all, let we understand that what are pandas series. Pandas Series are the type of array data structure. It is one dimensional data structure. It is capable of holding data of any type such as string, integer, float etc. A Series can be created using Series constructor. Syntax: pandas.Series(data, index, dtype, copy) Return: Series object.
1 min read
How to Convert Wide Dataframe to Tidy Dataframe with Pandas stack()?
We might sometimes need a tidy/long-form of data for data analysis. So, in python's library Pandas there are a few ways to reshape a dataframe which is in wide form into a dataframe in long/tidy form. Here, we will discuss converting data from a wide form into a long-form using the pandas function stack(). stack() mainly stacks the specified index
4 min read
Replace values of a DataFrame with the value of another DataFrame in Pandas
In this article, we will learn how we can replace values of a DataFrame with the value of another DataFrame using pandas. It can be done using the DataFrame.replace() method. It is used to replace a regex, string, list, series, number, dictionary, etc. from a DataFrame, Values of the DataFrame method are get replaced with another value dynamically.
4 min read
Converting Pandas Dataframe To Dask Dataframe
In this article, we will delve into the process of converting a Pandas DataFrame to a Dask DataFrame in Python through several straightforward methods. This conversion is particularly crucial when dealing with large datasets, as Dask provides parallel and distributed computing capabilities, allowing for efficient handling of substantial data volume
3 min read
Pandas Dataframe rank() | Rank DataFrame Entries
Python is a great language for data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.  Pandas DataFrame rank() method returns a rank of every respective entry (1 through n) along an axis of the DataFrame passed. The rank is retu
3 min read
Pandas DataFrame to_dict() Method | Convert DataFrame to Dictionary
Python is a great language for doing data analysis because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.  Pandas .to_dict() method is used to convert a DataFrame into a dictionary of series or list-like data type depending on the orient parameter. Exam
3 min read
Pandas DataFrame assign() Method | Create new Columns in DataFrame
Python is a great language for data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages, making importing and analyzing data much easier. The Dataframe.assign() method assigns new columns to a DataFrame, returning a new object (a copy) with the new columns added to the original one
4 min read
Python | Pandas DataFrame.fillna() to replace Null values in dataframe
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Sometimes csv file has null values, which are later displayed as NaN in Data Frame. Just like the pandas dropna() method manages and rem
5 min read
Pandas DataFrame hist() Method | Create Histogram in Pandas
A histogram is a graphical representation of the numerical data. Sometimes you'll want to share data insights with someone, and using graphical representations has become the industry standard. Pandas.DataFrame.hist() function plots the histogram of a given Data frame. It is useful in understanding the distribution of numeric variables. This functi
4 min read
Pandas DataFrame iterrows() Method | Pandas Method
Pandas DataFrame iterrows() iterates over a Pandas DataFrame rows in the form of (index, series) pair. This function iterates over the data frame column, it will return a tuple with the column name and content in the form of a series. Example: Python Code import pandas as pd df = pd.DataFrame({ 'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 32, 3
2 min read
Pandas DataFrame interpolate() Method | Pandas Method
Python is a great language for data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.  Python Pandas interpolate() method is used to fill NaN values in the DataFrame or Series using various interpolation techniques to fill the m
3 min read
Pandas DataFrame duplicated() Method | Pandas Method
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas duplicated() method identifies duplicated rows in a DataFrame. It returns a boolean series which is True only for unique rows. Ex
3 min read
Drop columns in DataFrame by label Names or by Index Positions
In this article, we will discuss how to drop columns in Pandas Dataframe by label Names or by Index Positions. Drop columns from a DataFrame can be achieved in multiple ways. Let's create a simple dataframe with a dictionary of lists, say column names are: 'Name', 'Age', 'Place', 'College'. # import pandas library as pd import pandas as pd # List o
5 min read
Selecting only numeric or string columns names from PySpark DataFrame
In this article, we will discuss how to select only numeric or string column names from a Spark DataFrame. Methods Used:createDataFrame: This method is used to create a spark DataFrame.isinstance: This is a Python function used to check if the specified object is of the specified type.dtypes: It returns a list of tuple (columnName,type). The return
2 min read
How to drop multiple column names given in a list from PySpark DataFrame ?
In this article, we are going to drop multiple columns given in the list in Pyspark dataframe in Python. For this, we will use the drop() function. This function is used to remove the value from dataframe. Syntax: dataframe.drop(*['column 1','column 2','column n']) Where, dataframe is the input dataframecolumn names are the columns passed through a
2 min read
How to change dataframe column names in PySpark ?
In this article, we are going to see how to change the column names in the pyspark data frame. Let's create a Dataframe for demonstration: C/C++ Code # Importing necessary libraries from pyspark.sql import SparkSession # Create a spark session spark = SparkSession.builder.appName('pyspark - example join').getOrCreate() # Create data in dataframe da
3 min read
Python | Pandas dataframe.get()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas dataframe.get() function is used to get item from object for given key. The key could be one or more than one dataframe column. I
2 min read
Get n-smallest values from a particular column in Pandas DataFrame
Pandas DataFrame is two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). Let's see how can we can get n-smallest values from a particular column in Pandas DataFrame. Observe this dataset first. We'll use 'Age', 'Weight' and 'Salary' columns of this data in order to get n-smallest value
1 min read
Get n-largest values from a particular column in Pandas DataFrame
Pandas DataFrame is two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). Let's see how can we can get n-largest values from a particular column in Pandas DataFrame. Observe this dataset first. We’ll use ‘Age’, ‘Weight’ and ‘Salary’ columns of this data in order to get n-largest values
1 min read
Get all rows in a Pandas DataFrame containing given substring
Let's see how to get all rows in a Pandas DataFrame containing given substring with the help of different examples. Code #1: Check the values PG in column Position # importing pandas import pandas as pd # Creating the dataframe with dict of lists df = pd.DataFrame({'Name': ['Geeks', 'Peter', 'James', 'Jack', 'Lisa'], 'Team': ['Boston', 'Boston', 'B
3 min read
Split a column in Pandas dataframe and get part of it
When a part of any column in Dataframe is important and the need is to take it separate, we can split a column on the basis of the requirement. We can use Pandas .str accessor, it does fast vectorized string operations for Series and Dataframes and returns a string object. Pandas str accessor has number of useful methods and one of them is str.spli
2 min read
Get the number of rows and number of columns in Pandas Dataframe
Pandas provide data analysts a variety of pre-defined functions to Get the number of rows and columns in a data frame. In this article, we will learn about the syntax and implementation of few such functions. Method 1: Using df.axes() Method axes() method in pandas allows to get the number of rows and columns in a go. It accepts the argument '0' fo
3 min read
Get minimum values in rows or columns with their index position in Pandas-Dataframe
Let’s discuss how to find minimum values in rows & columns of a Dataframe and also their index position. a) Find the minimum value among rows and columns : Dataframe.min() : This function returns the minimum of the values in the given object. If the input is a series, the method will return a scalar which will be the minimum of the values in the se
4 min read