Frequency of each value

Python Practice › Pandas Programs › Program 23

Frequency of each value

EasyPandas Programs · 23 of 95

Write a pandas program to count each value.

Input
[3, 0, 3, 2, 2, 0, 3, 3, 2]
Output
0    2
2    3
3    4
Name: count, dtype: int64

Approach

  1. value_counts().

Solution

Python
import pandas as pdprint(pd.Series([3, 0, 3, 2, 2, 0, 3, 3, 2]).value_counts().sort_index())
Output
0    2
2    3
3    4
Name: count, dtype: int64

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Minimum of a Series

Python Practice › Pandas Programs › Program 22

Minimum of a Series

EasyPandas Programs · 22 of 95

Write a pandas program to find the minimum value.

Input
[54, 38, 67, 87]
Output
38

Approach

  1. min().

Solution

Python
import pandas as pdprint(pd.Series([54, 38, 67, 87]).min())
Output
38

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Maximum of a Series

Python Practice › Pandas Programs › Program 21

Maximum of a Series

EasyPandas Programs · 21 of 95

Write a pandas program to find the maximum value.

Input
[54, 38, 67, 87]
Output
87

Approach

  1. max().

Solution

Python
import pandas as pdprint(pd.Series([54, 38, 67, 87]).max())
Output
87

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Items not in another Series

Python Practice › Pandas Programs › Program 20

Items not in another Series

EasyPandas Programs · 20 of 95

Write a pandas program to get the items of one Series not present in another.

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

Approach

  1. ~a.isin(b).

Solution

Python
import pandas as pda, b = pd.Series([4, 7, 3]), pd.Series([9, 4, 3])print(a[~a.isin(b)])
Output
1    7
dtype: int64

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Standard deviation of a Series

Python Practice › Pandas Programs › Program 19

Standard deviation of a Series

EasyPandas Programs · 19 of 95

Write a pandas program to find the standard deviation of a Series.

Input
[2, 4, 6, 8, 10]
Output
3.162278 2.828427

Approach

  1. std() uses the sample formula (ddof=1).

Solution

Python
import pandas as pds = pd.Series([2, 4, 6, 8, 10])print(round(s.std(), 6), round(s.std(ddof=0), 6))
Output
3.162278 2.828427

Line by line

Line 3

pandas divides by n – 1 by default; ddof=0 divides by n like NumPy.

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Mean of a Series

Python Practice › Pandas Programs › Program 18

Mean of a Series

EasyPandas Programs · 18 of 95

Write a pandas program to find the mean of a Series.

Input
[5, 4, 8, 6]
Output
5.75

Approach

  1. mean().

Solution

Python
import pandas as pdprint(pd.Series([5, 4, 8, 6]).mean())
Output
5.75

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Reorder the index

Python Practice › Pandas Programs › Program 17

Reorder the index

EasyPandas Programs · 17 of 95

Write a pandas program to change the order of a Series’ index.

Input
["Sqa", "tools", "Learning", "python", "Is", "fun"]
Output
2    Learning
3      python
0         Sqa
1       tools
4          Is
5         fun
dtype: str

Approach

  1. reindex with the new order.

Solution

Python
import pandas as pds = pd.Series(["Sqa", "tools", "Learning", "python", "Is", "fun"])print(s.reindex([2, 3, 0, 1, 4, 5]))
Output
2    Learning
3      python
0         Sqa
1       tools
4          Is
5         fun
dtype: str

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Subset by condition

Python Practice › Pandas Programs › Program 16

Subset by condition

EasyPandas Programs · 16 of 95

Write a pandas program to select the values below 40.

Input
[10, 25, 69, 74, 33, 54, 21]
Output
0    10
1    25
4    33
6    21
dtype: int64

Approach

  1. Boolean indexing.

Solution

Python
import pandas as pds = pd.Series([10, 25, 69, 74, 33, 54, 21])print(s[s < 40])
Output
0    10
1    25
4    33
6    21
dtype: int64

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Add data to a Series

Python Practice › Pandas Programs › Program 15

Add data to a Series

EasyPandas Programs · 15 of 95

Write a pandas program to add new values to an existing Series.

Input
["54", "27.90", "sqa", "33.33", "tools"] + ["python", "100"]
Output
0        54
1     27.90
2       sqa
3     33.33
4     tools
5    python
6       100
dtype: str

Approach

  1. pd.concat with a new Series.

Solution

Python
import pandas as pds = pd.Series(["54", "27.90", "sqa", "33.33", "tools"])s = pd.concat([s, pd.Series(["python", "100"])], ignore_index=True)print(s)
Output
0        54
1     27.90
2       sqa
3     33.33
4     tools
5    python
6       100
dtype: str

Line by line

Line 3

ignore_index=True renumbers the result 0 to 6.

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Sort a Series

Python Practice › Pandas Programs › Program 14

Sort a Series

EasyPandas Programs · 14 of 95

Write a pandas program to sort a Series.

Input
[55, 23, 10, 87]
Output
2    10
1    23
0    55
3    87
dtype: int64

Approach

  1. sort_values().

Solution

Python
import pandas as pdprint(pd.Series([55, 23, 10, 87]).sort_values())
Output
2    10
1    23
0    55
3    87
dtype: int64

Line by line

Line 2

The index labels move with their values.

Learn the concept

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

More pandas programs

← All 95 Pandas Programs