Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
Pandas provide a unique method to retrieve rows from a Data frame.
Dataframe.iloc method is used when the index label of a data frame is something other than numeric series of 0, 1, 2, 3….n or in case the user doesn’t know the index label. Rows can be extracted using an imaginary index position which isn’t visible in the data frame.
Index Position: Index position of rows in integer or list of integer.
Return type: Data frame or Series depending on parameters
To download the CSV used in code, click here.
Example #1: Extracting single row and comparing with .loc
In this example, same index number row is extracted by both .iloc and.loc method and compared. Since the index column by default is numeric, hence the index label will also be integers.
As shown in the output image, the results returned by both the methods are same.
Example #2: Extracting multiple rows with index
In this example, multiple rows are extracted first by passing a list and then by passing integers to extract rows between that range. After that, both the values are compared.
As shown in the output image, the results returned by both the methods are same. All values are True except values in college column since those were NaN values.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Select Rows & Columns by Name or Index in Pandas DataFrame using [ ], loc & iloc
- Python | Pandas Extracting rows using .loc
- Select any row from a Dataframe using iloc and iat in Pandas
- Python | Pandas Series.iloc
- Difference between loc() and iloc() in Pandas DataFrame
- Extracting MAC address using Python
- Extracting Tweets containing a particular Hashtag using Python
- Extracting email addresses using regular expressions in Python
- Using CountVectorizer to Extracting Features from Text
- Python - Extracting Kth Key in Dictionary
- Python - Extracting keys not in values
- Python - Extracting Key from Value Substring
- Python - Extracting Priority Elements in Tuple List
- Extracting an attribute value with beautifulsoup in Python
- NLP | Extracting Named Entities
- Extracting the real and imaginary parts of an NumPy array of complex numbers
- Web scraper for extracting emails based on keywords and regions
- Python | Delete rows/columns from DataFrame using Pandas.drop()
- Select first or last N rows in a Dataframe using head() and tail() method in Python-Pandas
- How to skip rows while reading csv file using Pandas?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.