Length of each word

Python Practice › Pandas Programs › Program 26

Length of each word

EasyPandas Programs · 26 of 95

Write a pandas program to count the characters of each word.

Input
["Virat", "Rohit", "Pant", "Shikhar"]
Output
0    5
1    5
2    4
3    7
dtype: int64

Approach

  1. str.len().

Solution

Python
import pandas as pdprint(pd.Series(["Virat", "Rohit", "Pant", "Shikhar"]).str.len())
Output
0    5
1    5
2    4
3    7
dtype: int64
Note

“Virat” and “Rohit” have 5 letters; the original sample showed 6.

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Capitalise first and last letters

Python Practice › Pandas Programs › Program 25

Capitalise first and last letters

EasyPandas Programs · 25 of 95

Write a pandas program to make the first and last letter of each word uppercase.

Input
["sqatools", "python", "data", "science"]
Output
0    SqatoolS
1      PythoN
2        DatA
3     SciencE
dtype: str

Approach

  1. map a small function.

Solution

Python
import pandas as pds = pd.Series(["sqatools", "python", "data", "science"])print(s.map(lambda w: w[0].upper() + w[1:-1] + w[-1].upper()))
Output
0    SqatoolS
1      PythoN
2        DatA
3     SciencE
dtype: str

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Items at positions

Python Practice › Pandas Programs › Program 24

Items at positions

EasyPandas Programs · 24 of 95

Write a pandas program to extract items at positions 1, 4 and 7.

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

Approach

  1. iloc with a list.

Solution

Python
import pandas as pds = pd.Series([3, 0, 3, 2, 2, 0, 3, 3, 2])print(s.iloc[[1, 4, 7]])
Output
1    0
4    2
7    3
dtype: int64

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

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