Select a row

Python Practice › Pandas Programs › Program 33

Select a row

EasyPandas Programs · 33 of 95

Write a pandas program to print a selected row.

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.

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Select columns

Python Practice › Pandas Programs › Program 32

Select columns

EasyPandas Programs · 32 of 95

Write a pandas program to print selected columns.

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

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Last n rows

Python Practice › Pandas Programs › Program 31

Last n rows

EasyPandas Programs · 31 of 95

Write a pandas program to print the last n rows.

Output
    n  square
8   9      81
9  10     100

Approach

  1. tail(n).

Solution

Python
import pandas as pddf = pd.DataFrame({"n": range(1, 11), "square": [i * i for i in range(1, 11)]})print(df.tail(2))
Output
    n  square
8   9      81
9  10     100

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

First n rows

Python Practice › Pandas Programs › Program 30

First n rows

EasyPandas Programs · 30 of 95

Write a pandas program to print the first n rows.

Output
   n  square
0  1       1
1  2       4
2  3       9

Approach

  1. head(n).

Solution

Python
import pandas as pddf = pd.DataFrame({"n": range(1, 11), "square": [i * i for i in range(1, 11)]})print(df.head(3))
Output
   n  square
0  1       1
1  2       4
2  3       9

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Dictionary to DataFrame

Python Practice › Pandas Programs › Program 29

Dictionary to DataFrame

EasyPandas Programs · 29 of 95

Write a pandas program to convert a dictionary into a DataFrame.

Output
    name  age
0   Alex   30
1   John   27
2  Peter   29

Approach

  1. Each key becomes a column.

Solution

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

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Index of smallest and largest

Python Practice › Pandas Programs › Program 28

Index of smallest and largest

EasyPandas Programs · 28 of 95

Write a pandas program to find the index of the smallest and largest values.

Input
[54, 25, 38, 87]
Output
1 3

Approach

  1. idxmin() and idxmax().

Solution

Python
import pandas as pds = pd.Series([54, 25, 38, 87])print(s.idxmin(), s.idxmax())
Output
1 3

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Date strings to datetimes

Python Practice › Pandas Programs › Program 27

Date strings to datetimes

EasyPandas Programs · 27 of 95

Write a pandas program to convert date strings in different formats into datetimes.

Input
["2 Feb 2020", "5/11/2021", "7-8-2022"]
Output
0   2020-02-02
1   2021-11-05
2   2022-08-07
dtype: datetime64[us]

Approach

  1. pd.to_datetime(..., format="mixed", dayfirst=True).

Solution

Python
import pandas as pds = pd.Series(["2 Feb 2020", "5/11/2021", "7-8-2022"])print(pd.to_datetime(s, format="mixed", dayfirst=True))
Output
0   2020-02-02
1   2021-11-05
2   2022-08-07
dtype: datetime64[us]

Line by line

Line 3

format="mixed" parses each value separately; dayfirst=True reads 5/11/2021 as 5 November.

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Length of each word

Python Practice › Pandas Programs › Program 26

Length of each word

EasyPandas Programs · 26 of 95

Write a pandas program to count the characters of each word.

Input
["Virat", "Rohit", "Pant", "Shikhar"]
Output
0    5
1    5
2    4
3    7
dtype: int64

Approach

  1. str.len().

Solution

Python
import pandas as pdprint(pd.Series(["Virat", "Rohit", "Pant", "Shikhar"]).str.len())
Output
0    5
1    5
2    4
3    7
dtype: int64
Note

“Virat” and “Rohit” have 5 letters; the original sample showed 6.

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Capitalise first and last letters

Python Practice › Pandas Programs › Program 25

Capitalise first and last letters

EasyPandas Programs · 25 of 95

Write a pandas program to make the first and last letter of each word uppercase.

Input
["sqatools", "python", "data", "science"]
Output
0    SqatoolS
1      PythoN
2        DatA
3     SciencE
dtype: str

Approach

  1. map a small function.

Solution

Python
import pandas as pds = pd.Series(["sqatools", "python", "data", "science"])print(s.map(lambda w: w[0].upper() + w[1:-1] + w[-1].upper()))
Output
0    SqatoolS
1      PythoN
2        DatA
3     SciencE
dtype: str

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Items at positions

Python Practice › Pandas Programs › Program 24

Items at positions

EasyPandas Programs · 24 of 95

Write a pandas program to extract items at positions 1, 4 and 7.

Input
[3, 0, 3, 2, 2, 0, 3, 3, 2]
Output
1    0
4    2
7    3
dtype: int64

Approach

  1. iloc with a list.

Solution

Python
import pandas as pds = pd.Series([3, 0, 3, 2, 2, 0, 3, 3, 2])print(s.iloc[[1, 4, 7]])
Output
1    0
4    2
7    3
dtype: int64

Learn the concept

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

More pandas programs

← All 95 Pandas Programs