Python

Pandas Access Row By Index

“Pandas is a library; we can say that it is an “open-source” library designed primarily for moving efficiently and logically with relational or labeled data. It offers a range of information structures and procedures for working with time series and quantitative information. The NumPy library serves as the foundation for this library. Pandas is quick and offers its users exceptional performance & efficiency. We have DataFrames in “pandas” that stores data in tabular form, and we can also access rows in “pandas” by their index values. Accessing rows by index means getting all the data of the desired row by putting its index value. In this tutorial, we will access the rows by index and will explain here which methods are used for accessing rows by index in Pandas.”

Methods for Accessing Row by Index in Pandas

“Pandas” provides two different methods for accessing the rows by index. We will discuss these methods here in this guide, and these methods are:

  • loc[ ] method.
  • iloc[ ] method.

Let’s do some codes and will utilize both methods separately and check the results of how these methods are utilized in “Pandas”.

Example 01: Using DataFrame.loc[ ] Method

We are opening the “Spyder” app for performing these examples, which are presented here in this tutorial. We are beginning with our first code here by importing the “pandas as pd”, which is the most important part here. We will access the “pandas” function by simply writing “pd”. Now, we have created a DataFrame and put some rows and columns in it. The name of this DataFrame is  “Report” here.

We are directed to add some data, and we put “Serial_No” as the first column name and add “1”, “2”, “3”, “4”,  and “5” to this “Serial_No” column. After this, we have added the “Students” column, and here we have “Smith”, “Jack”, “Joseph”, “Robert”, and “Cherry” in this column. The next column is named “Teachers,” and we added “Mia”, “Thomas”, “Emma”, “Charles”, and “Olivia”. Now comes the “Subject” column, and in this, we are adding “English”, “Maths”, “IT”, “Science”, and “Social” as the subjects. And the last column we have is the “Credit_Hrs” column. We put “3”, “4”, “4”, “5”, and “6” into the “Credit_Hrs” column. And convert all these data into the DataFrame below by putting “pd.DataFrame” and storing it in a new variable named “Report1”. After this, we print this “Report1” DataFrame on the console screen by utilizing “print ()”. Now we are setting the “index” on this DataFrame by utilizing the “set_index” method.

Here we choose “Serial_No” as the index. Below this, we are utilizing the “DataFrame.loc” method for getting the rows that we want. Here we put the name of the DataFrame, which is “Report1,” and also put the “Serail_No” whose rows we want to access. We select “2” from the “Serial_No” because we want to select the row whose “Serial_No” is “2”. And we store this row in the “result” variable. After this, we have “print” for rendering this row on the console screen of “Spyder”.

We simply press “Shift+Enter” and get this outcome of the code. Here,  it selects the row whose “Serial_No” is “2,” and this row contains “Jack”, “Thomas”, “Maths”, and “4”. We access this row by utilizing the “DataFrame.loc” method.

Example 02

We use the above code again, but in this example, we will select more than one row by utilizing the “DataFrame.loc” method. We are creating DataFrame, and after printing the DataFrame, we set the index. Then, we put two square brackets and placed two different “Credit_Hrs” in these square brackets, as shown. Here, we add “3, 5”, so it will return data of two rows whose “Credit_Hrs” are “3”, and “4”. We are storing both rows in the “rslt” variable and then printing them by using “print ()”.

This outcome shows that we get two rows here. The “Credit_Hrs” of the first row that we access here is “3,” and the second one is “5”, as we have selected “3” and “4” Credit_Hrs for accessing these two rows.

Example 03: Using DataFrame.iloc[ ] Method

Here we start this code and import the “pandas as pd”. The DataFrame’s name is “Stock” in this instance. We are instructed to add some data, so we name the first column as “Items” and enter the “Milk”, “Sponch”, “Ball”, “Rubber”, and “Puff” into this “Items” column. After that, we added the “Stationery” column, in which we include “Pointer”, “Register”, “Remover”, “Ink,” and “Marker”. The “Quality” column is next, and we are adding the quality values as “Good”, “Best”, “Superb”, “Average,” and “Good” to it.

We display the “Stock” with the support of the “print ()” statement. Now, there is the “DataFrame.iloc[ ]” method below, and we place the “2” index value for getting the index 2 row and save it as a “Data” variable. After this, we have a “print ()” which renders this row on the terminal of the “Spyder”.

The index 2 row contains three values which are “Ball”, “Remover”, and “Superb”. So, it accesses this row and renders below.

Example 04

Update example 3 here and select two index values for accessing three rows. The “Product” DataFrame is created here with the same data as in example 3 and also renders this. We choose three different index values here and put them in two square brackets. We choose “0”, “2”, and “4” index values. So, these three rows are selected and stored in the “Data1” variable. We render all three rows also by putting the “print ()” function here.

First, you can see the full DataFrame, and below, you can observe that it prints the selected rows as we access these three rows by consuming the “DataFrame.iloc[ ]” method.

Example 05

We again have a DataFrame here in this code with the name “Sale”. After displaying this “Sale” DataFrame, we directly put the “DataFrame.iloc” method in the print statement. So, it will access and also print the accessed rows. Here “:3” is utilized, which means we are accessing the first three rows here.

The output renders the whole DataFrame as well as the first three rows of the DataFrame below, as we have accessed these three rows by defining them in the “DataFrame.iloc[ ]” method.

Example 06

In this code, we will access the alternative rows. After defining and printing the original DataFrame, we place the “DataFrame.iloc” method as the parameter of the print statement and set “[::2]” in it, which represents that we want to access the alternative rows from this given DataFrame. It will access the alternative rows and also print them on the terminal.

Look at this output, and check that after displaying the full DataFrame, it accesses the alternative rows from this DataFrame and displays them below the original DataFrame.

Conclusion

We have written this tutorial to help you understand how to “access rows by index” in “pandas”. We have defined two methods here, which are the “DataFrame.loc[ ]” method and the “DataFrame.iloc[ ]” method, and also performed multiple examples in which we utilize both methods and access rows in different ways. We have also rendered the outputs along with the code scripts. We have explained each code in depth here. I hope you will easily learn how to access the rows by index and which methods are used for accessing the rows by index in “pandas”.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.