Select a row
Write a pandas program to print a selected row.
name age 2 John 27
Approach
ilocby position orlocby label.
Solution
import pandas as pddf = pd.DataFrame({"name": ["Alex", "John", "Peter"], "age": [30, 27, 29]}, index=[1, 2, 3])print(df.loc[[2]])name age 2 John 27
Line by line
Double brackets keep the result as a DataFrame.
Learn the concept
New to this topic? Read the lesson, then come back.