Python Practice › Pandas Programs › Program 33

Select a row

EasyPandas Programs · 33 of 95

Write a pandas program to print a selected row.

Advertisement
Output
   name  age
2  John   27

Approach

  1. iloc by position or loc by label.

Solution

Python
import pandas as pddf = pd.DataFrame({"name": ["Alex", "John", "Peter"], "age": [30, 27, 29]}, index=[1, 2, 3])print(df.loc[[2]])
Output
   name  age
2  John   27

Line by line

Line 3

Double brackets keep the result as a DataFrame.

Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More pandas programs

← All 95 Pandas Programs