Rows with missing age

Python Practice › Pandas Programs › Program 36

Rows with missing age

EasyPandas Programs · 36 of 95

Write a pandas program to select rows where age is missing.

Output
    name  age
1   John  NaN
3  Klaus  NaN

Approach

  1. isna().

Solution

Python
import numpy as npimport pandas as pddf = pd.DataFrame({"name": ["Alex", "John", "Peter", "Klaus"], "age": [30, np.nan, 29, np.nan]})print(df[df["age"].isna()])
Output
    name  age
1   John  NaN
3  Klaus  NaN

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Count rows and columns

Python Practice › Pandas Programs › Program 35

Count rows and columns

EasyPandas Programs · 35 of 95

Write a pandas program to count the rows and columns.

Output
No of rows: 4
No of columns: 2

Approach

  1. shape.

Solution

Python
import pandas as pddf = pd.DataFrame({"name": ["Alex", "John", "Peter", "Klaus"], "age": [30, 27, 29, 33]})rows, cols = df.shapeprint("No of rows:", rows)print("No of columns:", cols)
Output
No of rows: 4
No of columns: 2

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Rows where age > 29

Python Practice › Pandas Programs › Program 34

Rows where age > 29

EasyPandas Programs · 34 of 95

Write a pandas program to select the rows where age is greater than 29.

Output
    name  age
0   Alex   30
3  Klaus   33

Approach

  1. Boolean indexing.

Solution

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

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

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