Kodeclik Blog

How to extract the nth row in a Pandas dataframe

A Pandas dataframe is just like a table or spreadsheet, composed of columns and rows which contain data about some subject matter. Each column denotes typically one attribute (or variable) and each row contains one instance or observation of all variables.
For instance, here is a simple Pandas dataframe where the rows denote months and columns denote various facets of months:
The output will be:
Note that the days (rows) are numbered from 0 to 1.
If we wish to extract a specific row, we use the iloc() method that takes an index for the rowand returns the full row. For instance, if we wish to extract the March row, we will do:
The output is:
Note that the output is a series. If we wish to extract the output as a dataframe, we will provide the input as a list of integers rather than as a single integer:
The output is:
We can extract multiple rows by providing the list of indices:
The output is:
I.e., we have skipped Jan, Feb, and May because the only indices given were 2 and 3 (i.e., Mar and Apr).
Another way to extract multiple rows is by using the “:” operator, like so:
Here the notation means that we are interested in rows 1 (the second row) upto (but not including) the row with index 4. So we will get rows with indices 1, 2, and 3. The output is:
Similarly, if we did:
we will obtain:
i.e., all rows upto (but not including) the row indexed 4.
We can also index simultaneously rows and columns with the iloc() method. Here is an example of how that works:
Here we are printing the full dataframe followed by the slice of the dataframe with rows numbered 0 and 2 and columns numbered 1 and 2. We obtain:
In summary, extracting specific rows from a Pandas dataframe is easy using the iloc() method. Whether you wish just one entry or several at once—any task involving slicing through a pandas DataFrame can be accomplished easily with iloc! With these simple tools at your disposal now there should be nothing stopping you from organizing and extracting info from any dataset quickly and efficiently!
If you liked this blogpost, checkout our blogpost on Pandas daterange. Also learn how to get the column names from a Pandas dataframe. Finally, see how to print the first 10 rows of a dataframe.
Interested in more things Python? Checkout our post on Python queues. Also see our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!
Want to learn Python with us? Sign up for 1:1 or small group classes.

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.

Copyright @ Kodeclik 2024. All rights reserved.