How to Select Data with iloc function from DataFrame using Pandas Library in Jupyter Notebook

Posted on

Introduction

This article will show how to select data from a dataframe available in a script. The script itself runs in a Jupyter Notebook web-based application. Data available from any type of sources. The most important thing is that the retrieved data will be stored in a variable. That variable itself is a variable with DataFrame type. So, in order to display how to select data from a DataFrame, just run the Jupyter Notebook first. Run it with the following command execution :

(myenv) C:\python\data-science>jupyter notebook
[I 17:08:28.062 NotebookApp] Serving notebooks from local directory: C:\python\data-science
[I 17:08:28.062 NotebookApp] The Jupyter Notebook is running at:
[I 17:08:28.067 NotebookApp] http://localhost:8888/?token=4dd9801ef2aacad1d445955b0ae4621b4c669da84c617e7b
[I 17:08:28.068 NotebookApp]  or http://127.0.0.1:8888/?token=4dd9801ef2aacad1d445955b0ae4621b4c669da84c617e7b
[I 17:08:28.070 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 17:08:28.225 NotebookApp]
    To access the notebook, open this file in a browser:
        file:///C:/Users/Personal/AppData/Roaming/jupyter/runtime/nbserver-8796-open.html
    Or copy and paste one of these URLs:
        http://localhost:8888/?token=4dd9801ef2aacad1d445955b0ae4621b4c669da84c617e7b
     or http://127.0.0.1:8888/?token=4dd9801ef2aacad1d445955b0ae4621b4c669da84c617e7b

 

Data Selection from DataFrame Type Variable using iloc

After executing or running the Jupyter Notebook, just execute the suitable script. It is a script to select certain data available in the DataFrame variable. So, the following is a script to select data from a DataFrame variable using iloc function. For several reference, iloc is considered as the abbreviation integer- location based indexing or selection. So, using this function, it is possible to select certain data from the DataFrame using index as an integer value. The pattern for data selection from a DataFrame using iloc is possible only by feeding an integer value for row and column reference. That pattern exist as follows :

data.iloc[<row selection>, <column selection>]

Using the sample of data where exist in the article How to Get Data From a PostgreSQL Database in Jupyter Notebook, simulate how to select the data. The following is the example of the usage :

How to Select Data using iloc from DataFrame using Pandas Library in Jupyter Notebook

As in the above example, the variable with the name of data contains data from PostgreSQL database. That variable has a DataFrame data structure. Furthermore, the data selection from the data variable is a success. It select the row with index 0 and also the column with index 0. The output is 1. The output is correct since the row with index 0 is the first row. Furthermore, the column with index 0 is the first column which is the column with the label ‘id’. So, the value of the data from the first row and the first column is 1.

Leave a Reply