Replace a value

Python Practice › Pandas Programs › Program 44

Replace a value

EasyPandas Programs · 44 of 95

Write a pandas program to replace John with Jim.

Output
    name  age
0   Alex   30
1    Jim   27
2  Peter   29
3  Klaus   33

Approach

  1. replace() on the column.

Solution

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

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Sort by age

Python Practice › Pandas Programs › Program 43

Sort by age

EasyPandas Programs · 43 of 95

Write a pandas program to sort by age in ascending order.

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

Approach

  1. sort_values("age").

Solution

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

Mean of a column

Python Practice › Pandas Programs › Program 42

Mean of a column

EasyPandas Programs · 42 of 95

Write a pandas program to find the mean age.

Output
29.75

Approach

  1. mean().

Solution

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

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Sort by name

Python Practice › Pandas Programs › Program 41

Sort by name

EasyPandas Programs · 41 of 95

Write a pandas program to sort by name in ascending order.

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

Approach

  1. sort_values("name").

Solution

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

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Add a row

Python Practice › Pandas Programs › Program 40

Add a row

EasyPandas Programs · 40 of 95

Write a pandas program to add a new row.

Output
    name  age
0   Alex   30
1   John   27
2  Peter   29
3  Klaus   33
4  Jason   28

Approach

  1. df.loc[len(df)] = [...] or pd.concat.

Solution

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

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

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

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