Python Practice › Pandas Programs › Program 32

Select columns

EasyPandas Programs · 32 of 95

Write a pandas program to print selected columns.

Advertisement
Input
name and age from a table with Sr.no, name, age
Output
    name  age
0   Alex   30
1   John   27
2  Peter   29

Approach

  1. df[["name", "age"]].

Solution

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

Learn the concept

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

More pandas programs

← All 95 Pandas Programs