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

Round to two decimals

Python Practice › NumPy Programs › Program 96

Round to two decimals

EasyNumPy Programs · 96 of 100

Write a NumPy program to round elements to two decimal places.

Input
[4.8757, 2.4578]
Output
[4.88 2.46]

Approach

  1. np.round(a, 2).

Solution

Python
import numpy as npprint(np.round(np.array([4.8757, 2.4578]), 2))
Output
[4.88 2.46]
Note

Rounding gives 4.88 and 2.46; the original sample (4.86, 2.45) was not rounded correctly.

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Tangent of angles

Python Practice › NumPy Programs › Program 95

Tangent of angles

EasyNumPy Programs · 95 of 100

Write a NumPy program to calculate the tangent of angles.

Input
0, 45 degrees
Output
[0. 1.]

Approach

  1. np.tan(np.radians(...)).

Solution

Python
import numpy as npprint(np.tan(np.radians([0, 45])).round(3))
Output
[0. 1.]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Cosine of angles

Python Practice › NumPy Programs › Program 94

Cosine of angles

EasyNumPy Programs · 94 of 100

Write a NumPy program to calculate the cosine of angles.

Input
0, 60, 90 degrees
Output
[1.  0.5 0. ]

Approach

  1. np.cos(np.radians(...)).

Solution

Python
import numpy as npprint(np.cos(np.radians([0, 60, 90])).round(3))
Output
[1.  0.5 0. ]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Variance

Python Practice › NumPy Programs › Program 93

Variance

EasyNumPy Programs · 93 of 100

Write a NumPy program to calculate the variance of an array.

Input
[2, 4, 4, 4, 5, 5, 7, 9]
Output
4.0

Approach

  1. np.var.

Solution

Python
import numpy as npprint(np.var([2, 4, 4, 4, 5, 5, 7, 9]))
Output
4.0

Learn the concept

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

More numpy programs

← All 100 NumPy Programs