Pandas DataFrame - Get rows at specified list of indices
Pandas DataFrame - Get Rows at Specified List of Indices
To get rows at specified list of indices from a DataFrame, call DataFrame.take() function and pass the list of indices as an argument.
Syntax
The syntax to get rows at specified indices [index1, index2, index3]
from DataFrame df
is
df.take([index1, index2, index3])
Examples
1. Get rows in DataFrame at indices [1, 3, 4]
In this example, we take a DataFrame with five rows, and get the rows at indices [1, 3, 4]
using DataFrame.take() function.
Python Program
import pandas as pd
df = pd.DataFrame({'name':['a', 'b', 'c', 'd', 'e'],
'age' :[20, 21, 20, 19, 21]})
result = df.take([1, 3, 4])
print(result)
Output
name age
1 b 21
3 d 19
4 e 21
Summary
In this Pandas Tutorial, we learned how to get the rows at specified list of indices from a DataFrame.