
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to get nth row in a Pandas DataFrame?
To get the nth row in a Pandas DataFrame, we can use the iloc() method. For example, df.iloc[4] will return the 5th row because row numbers start from 0.
Steps
- Make two-dimensional, size-mutable, potentially heterogeneous tabular data, df.
- Print input DataFrame, df.
- Initialize a variable nth_row.
- Use iloc() method to get nth row.
- Print the returned DataFrame.
Example
import pandas as pd df = pd.DataFrame( dict( name=['John', 'Jacob', 'Tom', 'Tim', 'Ally'], marks=[89, 23, 100, 56, 90], subjects=["Math", "Physics", "Chemistry", "Biology", "English"] ) ) print "Input DataFrame is:\n", df nth_row = 3 df = df.iloc[nth_row] print "Row ", nth_row, "of the DataFrame is: \n", df
Output
Input DataFrame is: name marks subjects 0 John 89 Math 1 Jacob 23 Physics 2 Tom 100 Chemistry 3 Tim 56 Biology 4 Ally 90 English Row 3 of the DataFrame is: name Tim marks 56 subjects Biology Name: 3, dtype: object
- Related Articles
- How to get the row count of a Pandas DataFrame?
- Python Pandas - How to delete a row from a DataFrame
- Apply function to every row in a Pandas DataFrame
- Add a row at top in pandas DataFrame
- How to append a list as a row to a Pandas DataFrame in Python?
- How to get the nth percentile of a Pandas series?
- Apply function to every row in a Pandas DataFrame in Python
- Python Pandas - How to select rows from a DataFrame by passing row label
- How to get a value from the cell of a Pandas DataFrame?
- Deleting a DataFrame row in Python Pandas based on column value
- How to get the list of column headers from a Pandas DataFrame?
- Create a Pandas Dataframe by appending one row at a time
- Python - Change column names and row indexes in Pandas DataFrame
- How to get the sum of a specific column of a dataframe in Pandas Python?
- How to shift a column in a Pandas DataFrame?

Advertisements