Sum of a column

Python Practice › Pandas Programs › Program 39

Sum of a column

EasyPandas Programs · 39 of 95

Write a pandas program to sum the age column.

Output
119

Approach

  1. df["age"].sum().

Solution

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

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Change one value

Python Practice › Pandas Programs › Program 38

Change one value

EasyPandas Programs · 38 of 95

Write a pandas program to change John’s age to 24.

Output
    name  age
0   Alex   30
1   John   24
2  Peter   29
3  Klaus   33

Approach

  1. df.loc[condition, column] = value.

Solution

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

Line by line

Line 3

loc with a row condition and a column name changes the value in place.

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Ages between 25 and 30

Python Practice › Pandas Programs › Program 37

Ages between 25 and 30

EasyPandas Programs · 37 of 95

Write a pandas program to print rows where age is between 25 and 30.

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

Approach

  1. between(25, 30) includes both ends.

Solution

Python
import pandas as pddf = pd.DataFrame({"name": ["Alex", "John", "Peter", "Klaus"], "age": [30, 27, 29, 33]})print(df[df["age"].between(25, 30)])
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

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