{"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. 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 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 Output:<\/strong><\/p>\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 Output:<\/strong><\/p>\n If you want to get the data values of 2, 3 and 4th rows, then do as below:<\/p>\n Output:<\/strong><\/p>\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 Example:<\/strong><\/p>\n Output:<\/strong><\/p>\n In this article, we learned about the Python iloc() function and how it works.<\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" iloc[] Function in Python: 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. Pandas have a one-of-a-kind method for retrieving rows from a Data frame. When the index label of a data frame …<\/p>\n
\nUsing the index values assigned to it, we can retrieve a specific value from a row and column using the iloc() function.<\/p>\ndataframe.iloc[]<\/pre>\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
\n\n
\n \n<\/th>\n name<\/th>\n mfr<\/th>\n type<\/th>\n calories<\/th>\n protein<\/th>\n fat<\/th>\n sodium<\/th>\n fiber<\/th>\n carbo<\/th>\n sugars<\/th>\n potass<\/th>\n vitamins<\/th>\n shelf<\/th>\n weight<\/th>\n cups<\/th>\n rating<\/th>\n<\/tr>\n<\/thead>\n \n 0<\/th>\n 100% Bran<\/td>\n N<\/td>\n C<\/td>\n 70<\/td>\n 4<\/td>\n 1<\/td>\n 130<\/td>\n 10.0<\/td>\n 5.0<\/td>\n 6<\/td>\n 280<\/td>\n 25<\/td>\n 3<\/td>\n 1.0<\/td>\n 0.33<\/td>\n 68.402973<\/td>\n<\/tr>\n \n 1<\/th>\n 100% Natural Bran<\/td>\n Q<\/td>\n C<\/td>\n 120<\/td>\n 3<\/td>\n 5<\/td>\n 15<\/td>\n 2.0<\/td>\n 8.0<\/td>\n 8<\/td>\n 135<\/td>\n 0<\/td>\n 3<\/td>\n 1.0<\/td>\n 1.00<\/td>\n 33.983679<\/td>\n<\/tr>\n \n 2<\/th>\n All-Bran<\/td>\n K<\/td>\n C<\/td>\n 70<\/td>\n 4<\/td>\n 1<\/td>\n 260<\/td>\n 9.0<\/td>\n 7.0<\/td>\n 5<\/td>\n 320<\/td>\n 25<\/td>\n 3<\/td>\n 1.0<\/td>\n 0.33<\/td>\n 59.425505<\/td>\n<\/tr>\n \n 3<\/th>\n All-Bran with Extra Fiber<\/td>\n K<\/td>\n C<\/td>\n 50<\/td>\n 4<\/td>\n 0<\/td>\n 140<\/td>\n 14.0<\/td>\n 8.0<\/td>\n 0<\/td>\n 330<\/td>\n 25<\/td>\n 3<\/td>\n 1.0<\/td>\n 0.50<\/td>\n 93.704912<\/td>\n<\/tr>\n \n 4<\/th>\n Almond Delight<\/td>\n R<\/td>\n C<\/td>\n 110<\/td>\n 2<\/td>\n 2<\/td>\n 200<\/td>\n 1.0<\/td>\n 14.0<\/td>\n 8<\/td>\n -1<\/td>\n 25<\/td>\n 3<\/td>\n 1.0<\/td>\n 0.75<\/td>\n 34.384843<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\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
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
# 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
\n\n
\n \n<\/th>\n name<\/th>\n mfr<\/th>\n type<\/th>\n calories<\/th>\n protein<\/th>\n fat<\/th>\n sodium<\/th>\n fiber<\/th>\n carbo<\/th>\n sugars<\/th>\n potass<\/th>\n vitamins<\/th>\n shelf<\/th>\n weight<\/th>\n cups<\/th>\n rating<\/th>\n<\/tr>\n<\/thead>\n \n 2<\/th>\n All-Bran<\/td>\n K<\/td>\n C<\/td>\n 70<\/td>\n 4<\/td>\n 1<\/td>\n 260<\/td>\n 9.0<\/td>\n 7.0<\/td>\n 5<\/td>\n 320<\/td>\n 25<\/td>\n 3<\/td>\n 1.0<\/td>\n 0.33<\/td>\n 59.425505<\/td>\n<\/tr>\n \n 3<\/th>\n All-Bran with Extra Fiber<\/td>\n K<\/td>\n C<\/td>\n 50<\/td>\n 4<\/td>\n 0<\/td>\n 140<\/td>\n 14.0<\/td>\n 8.0<\/td>\n 0<\/td>\n 330<\/td>\n 25<\/td>\n 3<\/td>\n 1.0<\/td>\n 0.50<\/td>\n 93.704912<\/td>\n<\/tr>\n \n 4<\/th>\n Almond Delight<\/td>\n R<\/td>\n C<\/td>\n 110<\/td>\n 2<\/td>\n 2<\/td>\n 200<\/td>\n 1.0<\/td>\n 14.0<\/td>\n 8<\/td>\n -1<\/td>\n 25<\/td>\n 3<\/td>\n 1.0<\/td>\n 0.75<\/td>\n 34.384843<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n dataframe.iloc[:, startcolumn : endcolumn]<\/pre>\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
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
\n