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

Series of lists to one Series

Python Practice › Pandas Programs › Program 13

Series of lists to one Series

EasyPandas Programs · 13 of 95

Write a pandas program to turn a Series of lists into one flat Series.

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

Approach

  1. explode() puts each list item on its own row.

Solution

Python
import pandas as pds = pd.Series([["Sqa", "tool"], ["Learning", "python"], ["Is", "fun"]])print(s.explode().reset_index(drop=True))
Output
0         Sqa
1        tool
2    Learning
3      python
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

Series to array

Python Practice › Pandas Programs › Program 12

Series to array

EasyPandas Programs · 12 of 95

Write a pandas program to convert a Series to a NumPy array.

Input
["54", "27.90", "sqa", "33.33", "tools"]
Output
['54' '27.90' 'sqa' '33.33' 'tools']

Approach

  1. to_numpy().

Solution

Python
import pandas as pdprint(pd.Series(["54", "27.90", "sqa", "33.33", "tools"]).to_numpy())
Output
['54' '27.90' 'sqa' '33.33' 'tools']

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Convert a Series to numbers

Python Practice › Pandas Programs › Program 11

Convert a Series to numbers

EasyPandas Programs · 11 of 95

Write a pandas program to convert a mixed Series to numbers, turning text into NaN.

Input
["54", "27.90", "sqa", "33.33", "tools"]
Output
0    54.00
1    27.90
2      NaN
3    33.33
4      NaN
dtype: float64

Approach

  1. pd.to_numeric(..., errors="coerce").

Solution

Python
import pandas as pds = pd.Series(["54", "27.90", "sqa", "33.33", "tools"])print(pd.to_numeric(s, errors="coerce"))
Output
0    54.00
1    27.90
2      NaN
3    33.33
4      NaN
dtype: float64

Line by line

Line 3

errors="coerce" turns values that are not numbers into NaN instead of raising an error.

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

NumPy array to Series

Python Practice › Pandas Programs › Program 10

NumPy array to Series

EasyPandas Programs · 10 of 95

Write a pandas program to convert a NumPy array to a Series.

Input
[5, 3, 8, 9, 0]
Output
0    5
1    3
2    8
3    9
4    0
dtype: int64

Approach

  1. pd.Series(array).

Solution

Python
import numpy as npimport pandas as pdprint(pd.Series(np.array([5, 3, 8, 9, 0])))
Output
0    5
1    3
2    8
3    9
4    0
dtype: int64

Learn the concept

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

More pandas programs

← All 95 Pandas Programs