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

Series to list

Series to list

EasyPandas Programs · 2 of 95

Write a pandas program to convert a Series to a list and print its type.

Output
[15, 43, 88] list

Approach

  1. tolist().

Solution

Python
import pandas as pditems = pd.Series([15, 43, 88]).tolist()print(items, type(items).__name__)
Output
[15, 43, 88] list

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Create a Series

Create a Series

EasyPandas Programs · 1 of 95

Write a pandas program to create and display a one-dimensional Series.

Input
[15, 43, 88, 23]
Output
0    15
1    43
2    88
3    23
dtype: int64

Approach

  1. pd.Series(list); pandas adds an index 0, 1, 2, …

Solution

Python
import pandas as pds = pd.Series([15, 43, 88, 23])print(s)
Output
0    15
1    43
2    88
3    23
dtype: int64

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Complex dtype array

Python Practice › NumPy Programs › Program 100

Complex dtype array

EasyNumPy Programs · 100 of 100

Write a NumPy program to create an array with complex dtype.

Input
[5, 4]
Output
[5.+0.j 4.+0.j]

Approach

  1. dtype=complex.

Solution

Python
import numpy as npprint(np.array([5, 4], dtype=complex))
Output
[5.+0.j 4.+0.j]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Tuple to array

Python Practice › NumPy Programs › Program 99

Tuple to array

EasyNumPy Programs · 99 of 100

Write a NumPy program to convert a tuple into an array.

Input
(5, 7, 0)
Output
[5 7 0]

Approach

  1. np.array(tuple).

Solution

Python
import numpy as npprint(np.array((5, 7, 0)))
Output
[5 7 0]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Ceiling of each element

Python Practice › NumPy Programs › Program 98

Ceiling of each element

EasyNumPy Programs · 98 of 100

Write a NumPy program to round elements up with ceil.

Input
[4.8757, 2.4578]
Output
[5. 3.]

Approach

  1. np.ceil.

Solution

Python
import numpy as npprint(np.ceil([4.8757, 2.4578]))
Output
[5. 3.]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Floor of each element

Python Practice › NumPy Programs › Program 97

Floor of each element

EasyNumPy Programs · 97 of 100

Write a NumPy program to round elements down with floor.

Input
[4.8757, 2.4578]
Output
[4. 2.]

Approach

  1. np.floor.

Solution

Python
import numpy as npprint(np.floor([4.8757, 2.4578]))
Output
[4. 2.]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs