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

Dictionary to Series

Dictionary to Series

EasyPandas Programs · 9 of 95

Write a pandas program to convert a dictionary to a Series.

Input
{"name": "Virat", "sport": "cricket", "age": 32}
Output
name       Virat
sport    cricket
age           32
dtype: object

Approach

  1. Keys become the index.

Solution

Python
import pandas as pdprint(pd.Series({"name": "Virat", "sport": "cricket", "age": 32}))
Output
name       Virat
sport    cricket
age           32
dtype: object

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Are Series elements equal?

Are Series elements equal?

EasyPandas Programs · 7 of 95

Write a pandas program to compare two Series element by element.

Input
[1, 6, 9, 5], [5, 2, 4, 5]
Output
[False, False, False, True]

Approach

  1. ==.

Solution

Python
import pandas as pda, b = pd.Series([1, 6, 9, 5]), pd.Series([5, 2, 4, 5])print((a == b).tolist())
Output
[False, False, False, True]

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Divide two Series

Divide two Series

EasyPandas Programs · 6 of 95

Write a pandas program to divide two Series.

Input
[1, 6, 9, 5], [5, 2, 4, 5]
Output
[0.2, 3.0, 2.25, 1.0]

Approach

  1. / always gives floats.

Solution

Python
import pandas as pda, b = pd.Series([1, 6, 9, 5]), pd.Series([5, 2, 4, 5])print((a / b).tolist())
Output
[0.2, 3.0, 2.25, 1.0]
Note

9 / 4 is 2.25; the original sample showed 2.5.

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Multiply two Series

Multiply two Series

EasyPandas Programs · 5 of 95

Write a pandas program to multiply two Series.

Input
[1, 6, 9, 5], [5, 2, 4, 5]
Output
[5, 12, 36, 25]

Approach

  1. *.

Solution

Python
import pandas as pda, b = pd.Series([1, 6, 9, 5]), pd.Series([5, 2, 4, 5])print((a * b).tolist())
Output
[5, 12, 36, 25]

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Subtract two Series

Subtract two Series

EasyPandas Programs · 4 of 95

Write a pandas program to subtract two Series.

Input
[1, 6, 9, 5], [5, 2, 4, 5]
Output
[-4, 4, 5, 0]

Approach

  1. -.

Solution

Python
import pandas as pda, b = pd.Series([1, 6, 9, 5]), pd.Series([5, 2, 4, 5])print((a - b).tolist())
Output
[-4, 4, 5, 0]

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Add two Series

Add two Series

EasyPandas Programs · 3 of 95

Write a pandas program to add two Series.

Input
[1, 6, 9, 5], [5, 2, 4, 5]
Output
[6, 8, 13, 10]

Approach

  1. + adds values with the same index.

Solution

Python
import pandas as pda, b = pd.Series([1, 6, 9, 5]), pd.Series([5, 2, 4, 5])print((a + b).tolist())
Output
[6, 8, 13, 10]

Learn the concept

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

More pandas programs

← All 95 Pandas Programs