Chapter 26 pandas Exercises
26.0.1 Question
Write a Pandas program to create and display a one-dimensional array-like object containing an array of data using Pandas module.
import pandas as pd
# Create a Pandas Series
data = [10, 20, 30, 40, 50]
series = pd.Series(data)
# Display the Series
print(series)## 0 10
## 1 20
## 2 30
## 3 40
## 4 50
## dtype: int64
26.0.2 Question
Write a Pandas program to convert a Pandas module Series to Python list and it’s type.
import pandas as pd
data = pd.Series([1, 2, 3, 4, 5])
# Convert series to python list
data_list = list(data) ### general solution
data_list = data.to_list() ### pandas optimized
print(type(data_list))## <class 'list'>
26.0.3 Question
Write a Pandas program to add, subtract, multiple and divide two Pandas Series.
serias_a = pd.Series([2, 4, 6, 8, 10])
serias_b = pd.Series([1, 3, 5, 7, 9])
# addition
print('addition\n', serias_a + serias_b)## addition
## 0 3
## 1 7
## 2 11
## 3 15
## 4 19
## dtype: int64
##
## subtraction
## 0 1
## 1 1
## 2 1
## 3 1
## 4 1
## dtype: int64
##
## multiplication
## 0 2
## 1 12
## 2 30
## 3 56
## 4 90
## dtype: int64
##
## division
## 0 2.000000
## 1 1.333333
## 2 1.200000
## 3 1.142857
## 4 1.111111
## dtype: float64
26.0.4 Question
Write a Pandas program to compare the elements of the two Pandas Series.
## 0 False
## 1 False
## 2 False
## 3 False
## 4 True
## dtype: bool
## 0 True
## 1 True
## 2 True
## 3 True
## 4 False
## dtype: bool
## 0 False
## 1 False
## 2 False
## 3 False
## 4 False
## dtype: bool
## 0 True
## 1 True
## 2 True
## 3 True
## 4 False
## dtype: bool