Add a column

Python Practice › Pandas Programs › Program 46

Add a column

EasyPandas Programs · 46 of 95

Write a pandas program to add a salary column.

Output
    name  age  salary
0   Alex   30   50000
1   John   27   65000
2  Peter   29   58000
3  Klaus   33   66000

Approach

  1. Assign a list to a new column name.

Solution

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

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Delete rows

Python Practice › Pandas Programs › Program 45

Delete rows

EasyPandas Programs · 45 of 95

Write a pandas program to delete John’s record.

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

Approach

  1. Keep the rows that do not match.

Solution

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

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