Indexing in Pandas Dataframe using Python | by Kaushik Katari | Towards Data Science

In Numpy arrays, we are familiar with the concepts of indexing, slicing, and masking, etc. Similarly, Pandas to supports indexing in their Dataframe. If we are familiar with the indexing in Numpy arrays, the indexing in Pandas will be very easy.
What is Indexing in Python?
Selecting values from particular rows and columns in a dataframe is known as Indexing. By using Indexing, we can select all rows and some columns or some rows and all columns.
Let’s create a sample data in a series form for better understanding of indexing.
The output series looks like this,
1    a3    b5    cdtype: object
Now, here Python offers two types of indices
Explicit
Implicit
Explicit Indexing:
For the above dataset if we pass the command as,
ds[1] it uses explicit indices
# If we pass the above command ds[1], the output will be'a'
This is Explicit Indexing. Whereas, if we pass the command ds[1:3] it will use the implicit index style,
The output for the command ds[1:3] will be,
3    b5    cdtype: object
These slicing and indexing can lead to some sort of confusion. To avoid this, Python offers some special indexer attributes:
loc
The loc attribute allows indexing and slicing that always references the explicit index
iloc
The iloc attribute allows indexing and slicing that always references the implicit index style
One common expression in Python code that everyone follows and practices is “explicit is better than implicit.”
Let’s take a sample dataset and see how indexing can be performed in different formats.
We are using the data of NBA players from kaggle.
The Dataset looks like this,
To display a single column from the dataframe, we will mention the column name in the print statement.
The output will look like this,
Let’s try to display the ‘Age’, ‘College’ and ‘Draft Year’ of the players. We can display multiple columns in the following way,
The multiple columns will display like this,
Indexing using .loc method. If we use the .loc method, we have to pass the data using its Label name.
To display a single row from the dataframe, we will mention the row’s index name in the .loc method.
The whole row information will display like this,
Same as single row, pass the rows information in the print command to display the information.
The output will be,
We can also select multiple rows and multiple columns at a time using the .loc method.
The output will be like this,
To display all the rows with some columns using the .loc method.
The output of the above code will be like this,
The same output can be achieved by simply giving column names without using the .loc method as shown in Selecting Multiple Columns.
The output will be same as the one above,
Indexing using the .iloc method. If we use the .iloc method, we have to pass the data using its Position. It is very similar to the .loc method, the only difference is .iloc uses integers to extract the information.
We have to pass a single integer in the .iloc method to get the row information.
The output will be,
To select multiple rows, we have to pass the positions of the selected rows.
The output will look something like this,
To display a specific number of rows and columns, we create a list of integer for rows and a list of integer for columns and pass to iloc function.
The output will be,
To display all the rows, we have to pass “:” and the integers for columns.
The output will look like something like this,
If the columns in the dataset are of “int” or “float” type, then we can apply all the numeric operations to the column directly and manipulate the data to our requirements.
The output will be like this,
The output will be same as the previous one with .loc method
We can conclude this article in three simple statements.
To avoid confusion on Explicit Indices and Implicit Indices we use .loc and .iloc methods.
.loc method is used for label based indexing.
.iloc method is used for position based indexing.
These are the three main statements, we need to be aware of while using indexing methods for a Pandas Dataframe in Python.
Thank you for reading and Happy Coding!!!
Seaborn: Python
Pandas: Python
Matplotlib: Python
NumPy: Python
Data Visualization and its Importance: Python
Time Complexity and Its Importance in Python
Python Recursion or Recursive Function in Python
Data Indexing and Selection: https://jakevdp.github.io/PythonDataScienceHandbook/03.02-data-indexing-and-selection.html
Indexing, Slicing and Sub-setting DataFrames in Python: https://datacarpentry.org/python-ecology-lesson/03-index-slice-subset/index.html
loc and iloc: https://campus.datacamp.com/courses/intermediate-python/dictionaries-pandas?ex=17
Using iloc, loc, & ix to select rows and columns in Pandas DataFrames: https://www.shanelynn.ie/select-pandas-dataframe-rows-and-columns-using-iloc-loc-and-ix/