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

Is one Series greater?

Is one Series greater?

EasyPandas Programs · 8 of 95

Write a pandas program to check element-wise whether one Series is greater than another.

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

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, True, True, False]

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

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