How to get specific row in Pandas DataFrame using index?
Pandas DataFrame - Get Specific Row using Index
To get the specific row of Pandas DataFrame using index, use DataFrame.iloc property and give the index of row in square brackets.
DataFrame.iloc[row_index]
DataFrame.iloc returns the row as Series object.
Examples
1. Get specific row in DataFrame
In this example, we will
- Initialize a DataFrame with some numbers.
- Get the specific row (index = 1) using DataFrame.iloc property.
Python Program
import pandas as pd
import numpy as np
df = pd.DataFrame(
[['a', 'b', 'c'],
['d', 'e', 'f'],
['g', 'h', 'i'],
['j', 'k', 'l']])
row = df.iloc[1] #index=1 => second row
print(row)
Output
0 d
1 e
2 f
Name: 1, dtype: object
In the output: 0, 1 and 2 is the column index; and d, e, f is the row.
Summary
In this tutorial of Python Examples, we learned how to get a specific row from Pandas DataFrame using DataFrame.iloc property.