{"id":26240,"date":"2022-01-03T09:13:27","date_gmt":"2022-01-03T03:43:27","guid":{"rendered":"https:\/\/python-programs.com\/?p=26240"},"modified":"2022-01-03T09:13:27","modified_gmt":"2022-01-03T03:43:27","slug":"python-iloc-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-iloc-function-with-examples\/","title":{"rendered":"Python iloc() Function with Examples"},"content":{"rendered":"

iloc[] Function in Python:<\/strong><\/p>\n

Python is a fantastic language for data analysis, owing to its fantastic ecosystem of data-centric Python packages. Pandas is one of these packages, and it greatly simplifies data import and analysis.<\/p>\n

Pandas have a one-of-a-kind method for retrieving rows from a Data frame. When the index label of a data frame is something other than a numeric series of 0, 1, 2, 3….n, or when the user does not know the index label, the Dataframe.iloc[] method is used. Rows can be extracted by using an imaginary index position that is not visible in the data frame.<\/p>\n

The Python iloc() function allows us to select a specific cell of a dataset, that is, to select a value from a set of values in a data frame or dataset that belongs to a specific row or column.
\nUsing the index values assigned to it, we can retrieve a specific value from a row and column using the iloc() function.<\/p>\n

Keep in mind that the iloc() function only accepts integer type values as index values for the values to be accessed and displayed.<\/p>\n

As previously stated, boolean values cannot be used as an index to retrieve records. It must be supplied with integer values.<\/p>\n

Syntax:<\/strong><\/p>\n

dataframe.iloc[]<\/pre>\n

For Example:<\/strong><\/p>\n

Let us take the first 5 rows of the dataset to understand the dataframe.iloc[] function<\/p>\n

Apply head() function to the above dataset to get the first 5 rows.<\/p>\n

# Import pandas module as pd using the import keyword\r\nimport pandas as pd\r\n# Import dataset using read_csv() function by pasing the dataset name as\r\n# an argument to it.\r\n# Store it in a variable.\r\ncereal_dataset = pd.read_csv('cereal.csv')\r\n# Apply head() function to the above dataset to get the first 5 rows.\r\ncereal_dataset.head()<\/pre>\n

Output:<\/strong><\/p>\n\n\n\n\n\n\n\n\n\n
<\/th>\nname<\/th>\nmfr<\/th>\ntype<\/th>\ncalories<\/th>\nprotein<\/th>\nfat<\/th>\nsodium<\/th>\nfiber<\/th>\ncarbo<\/th>\nsugars<\/th>\npotass<\/th>\nvitamins<\/th>\nshelf<\/th>\nweight<\/th>\ncups<\/th>\nrating<\/th>\n<\/tr>\n<\/thead>\n
0<\/th>\n100% Bran<\/td>\nN<\/td>\nC<\/td>\n70<\/td>\n4<\/td>\n1<\/td>\n130<\/td>\n10.0<\/td>\n5.0<\/td>\n6<\/td>\n280<\/td>\n25<\/td>\n3<\/td>\n1.0<\/td>\n0.33<\/td>\n68.402973<\/td>\n<\/tr>\n
1<\/th>\n100% Natural Bran<\/td>\nQ<\/td>\nC<\/td>\n120<\/td>\n3<\/td>\n5<\/td>\n15<\/td>\n2.0<\/td>\n8.0<\/td>\n8<\/td>\n135<\/td>\n0<\/td>\n3<\/td>\n1.0<\/td>\n1.00<\/td>\n33.983679<\/td>\n<\/tr>\n
2<\/th>\nAll-Bran<\/td>\nK<\/td>\nC<\/td>\n70<\/td>\n4<\/td>\n1<\/td>\n260<\/td>\n9.0<\/td>\n7.0<\/td>\n5<\/td>\n320<\/td>\n25<\/td>\n3<\/td>\n1.0<\/td>\n0.33<\/td>\n59.425505<\/td>\n<\/tr>\n
3<\/th>\nAll-Bran with Extra Fiber<\/td>\nK<\/td>\nC<\/td>\n50<\/td>\n4<\/td>\n0<\/td>\n140<\/td>\n14.0<\/td>\n8.0<\/td>\n0<\/td>\n330<\/td>\n25<\/td>\n3<\/td>\n1.0<\/td>\n0.50<\/td>\n93.704912<\/td>\n<\/tr>\n
4<\/th>\nAlmond Delight<\/td>\nR<\/td>\nC<\/td>\n110<\/td>\n2<\/td>\n2<\/td>\n200<\/td>\n1.0<\/td>\n14.0<\/td>\n8<\/td>\n-1<\/td>\n25<\/td>\n3<\/td>\n1.0<\/td>\n0.75<\/td>\n34.384843<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

If you want to retrieve all of the data values from the 2nd index of each column of the dataset, do as shown below:<\/p>\n

# Import pandas module as pd using the import keyword\r\nimport pandas as pd\r\n# Import numpy module as np using the import keyword\r\nimport numpy as np\r\n# Import os module using the import keyword\r\nimport os\r\n# Import dataset using read_csv() function by pasing the dataset name as\r\n# an argument to it.\r\n# Store it in a variable.\r\ncereal_dataset = pd.read_csv('cereal.csv')\r\n# Apply iloc() function to the above dataset to get all of the data values \r\n# from the 2nd index of each column and print it.\r\nprint(cereal_dataset.iloc[2])\r\n<\/pre>\n

Output:<\/strong><\/p>\n

name        All-Bran\r\nmfr                K\r\ntype               C\r\ncalories          70\r\nprotein            4\r\nfat                1\r\nsodium           260\r\nfiber              9\r\ncarbo              7\r\nsugars             5\r\npotass           320\r\nvitamins          25\r\nshelf              3\r\nweight             1\r\ncups            0.33\r\nrating       59.4255\r\nName: 2, dtype: object<\/pre>\n

If you want to get the data values of 2, 3 and 4th rows, then do as below:<\/p>\n

# Import pandas module as pd using the import keyword\r\nimport pandas as pd\r\n# Import numpy module as np using the import keyword\r\nimport numpy as np\r\n# Import os module using the import keyword\r\nimport os\r\n# Import dataset using read_csv() function by pasing the dataset name as\r\n# an argument to it.\r\n# Store it in a variable.\r\ncereal_dataset = pd.read_csv('cereal.csv')\r\n# Apply iloc() function to the above dataset to get the data values of 2, 3 and 4th rows\r\n# using slicing (It excludes the last row i.e, 5)\r\ncereal_dataset.iloc[2:5]\r\n<\/pre>\n

Output:<\/strong><\/p>\n\n\n\n\n\n\n\n
<\/th>\nname<\/th>\nmfr<\/th>\ntype<\/th>\ncalories<\/th>\nprotein<\/th>\nfat<\/th>\nsodium<\/th>\nfiber<\/th>\ncarbo<\/th>\nsugars<\/th>\npotass<\/th>\nvitamins<\/th>\nshelf<\/th>\nweight<\/th>\ncups<\/th>\nrating<\/th>\n<\/tr>\n<\/thead>\n
2<\/th>\nAll-Bran<\/td>\nK<\/td>\nC<\/td>\n70<\/td>\n4<\/td>\n1<\/td>\n260<\/td>\n9.0<\/td>\n7.0<\/td>\n5<\/td>\n320<\/td>\n25<\/td>\n3<\/td>\n1.0<\/td>\n0.33<\/td>\n59.425505<\/td>\n<\/tr>\n
3<\/th>\nAll-Bran with Extra Fiber<\/td>\nK<\/td>\nC<\/td>\n50<\/td>\n4<\/td>\n0<\/td>\n140<\/td>\n14.0<\/td>\n8.0<\/td>\n0<\/td>\n330<\/td>\n25<\/td>\n3<\/td>\n1.0<\/td>\n0.50<\/td>\n93.704912<\/td>\n<\/tr>\n
4<\/th>\nAlmond Delight<\/td>\nR<\/td>\nC<\/td>\n110<\/td>\n2<\/td>\n2<\/td>\n200<\/td>\n1.0<\/td>\n14.0<\/td>\n8<\/td>\n-1<\/td>\n25<\/td>\n3<\/td>\n1.0<\/td>\n0.75<\/td>\n34.384843<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

For columns:<\/strong><\/p>\n

If you want to get the data values of 2 and 3 rd columns, then do as below:<\/p>\n

Syntax:<\/strong><\/p>\n

dataframe.iloc[:, startcolumn : endcolumn]<\/pre>\n

Example:<\/strong><\/p>\n

# Import pandas module as pd using the import keyword\r\nimport pandas as pd\r\n# Import numpy module as np using the import keyword\r\nimport numpy as np\r\n# Import os module using the import keyword\r\nimport os\r\n# Import dataset using read_csv() function by pasing the dataset name as\r\n# an argument to it.\r\n# Store it in a variable.\r\ncereal_dataset = pd.read_csv('cereal.csv')\r\n# Apply iloc() function to the above dataset to get the data values of 2 and 3rd columns\r\n# using slicing (It excludes the last column i.e, 4)\r\ncereal_dataset.iloc[:,2:4]\r\n<\/pre>\n

Output:<\/strong><\/p>\n

      type     calories\r\n0\tC\t70\r\n1\tC\t120\r\n2\tC\t70\r\n3\tC\t50\r\n4\tC\t110\r\n...\t...\t...\r\n72\tC\t110\r\n73\tC\t110\r\n74\tC\t100\r\n75\tC\t100\r\n76\tC\t110<\/pre>\n
Brief Recall:<\/h5>\n

In this article, we learned about the Python iloc() function and how it works.<\/p>\n