1.
Write a Python program to check whether a JSON string contains complex
object or not.
PROGRAM:
import json
def is_complex_num(objct):
if '__complex__' in objct:
return complex(objct['real'], objct['img'])
return objct
complex_object =json.loads('{"__complex__": true, "real": 4, "img":
5}', object_hook = is_complex_num)
simple_object =json.loads('{"real": 4, "img": 3}', object_hook =
is_complex_num)
print("Complex_object: ",complex_object)
print("Without complex object: ",simple_object)
OUTPUT:
Complex_object: (4+5j)
Without complex object: {'real': 4, 'img': 3}
2. Python Program to demonstrate NumPy arrays creation using array () function.
PROGRAM:
import numpy as np
# Create a 1D NumPy array from a list
array_1d = np.array([1, 2, 3, 4, 5])
print("1D Array:")
print(array_1d)
# Create a 2D NumPy array (matrix) from a list of lists
array_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print("\n2D Array:")
print(array_2d)
# Create a 3D NumPy array (tensor) from a list of lists of lists
array_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print("\n3D Array:")
print(array_3d)
# Create a NumPy array from a tuple
array_from_tuple = np.array((10, 20, 30, 40))
print("\nArray from Tuple:")
print(array_from_tuple)
# Create an array with different data types (float numbers)
array_float = np.array([1.1, 2.2, 3.3, 4.4], dtype=float)
print("\nArray with Float Numbers:")
print(array_float)
# Create a 2D array with different data types (integers and floats)
array_mixed = np.array([[1, 2, 3], [4.4, 5.5, 6.6]], dtype=object)
print("\n2D Array with Mixed Data Types:")
print(array_mixed)
OUTPUT:
1D Array:
[1 2 3 4 5]
2D Array:
[[1 2 3]
[4 5 6]
[7 8 9]]
3D Array:
[[[1 2]
[3 4]]
[[5 6]
[7 8]]]
Array from Tuple:
[10 20 30 40]
Array with Float Numbers:
[1.1 2.2 3.3 4.4]
2D Array with Mixed Data Types:
[[1 2 3]
[4.4 5.5 6.6]]
3. Python program to demonstrate use of ndim, shape, size, dtype.
PROGRAM:
import numpy as np
# Create a NumPy array
array_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# Demonstrate the use of ndim, shape, size, and dtype
print("Array:")
print(array_2d)
# Number of dimensions
print("\nNumber of dimensions (ndim):", array_2d.ndim)
# Shape of the array
print("Shape of the array (shape):", array_2d.shape)
# Total number of elements
print("Total number of elements (size):", array_2d.size)
# Data type of the array elements
print("Data type of array elements (dtype):", array_2d.dtype)
# Create another array with a different data type (float)
array_float = np.array([[1.5, 2.5], [3.5, 4.5]])
print("\nArray with float values:")
print(array_float)
print("\nNumber of dimensions (ndim):", array_float.ndim)
print("Shape of the array (shape):", array_float.shape)
print("Total number of elements (size):", array_float.size)
print("Data type of array elements (dtype):", array_float.dtype)
OUTPUT:
Array:
[[1 2 3]
[4 5 6]
[7 8 9]]
Number of dimensions (ndim): 2
Shape of the array (shape): (3, 3)
Total number of elements (size): 9
Data type of array elements (dtype): int64
Array with float values:
[[1.5 2.5]
[3.5 4.5]]
Number of dimensions (ndim): 2
Shape of the array (shape): (2, 2)
Total number of elements (size): 4
Data type of array elements (dtype): float64
4. Python program to demonstrate basic slicing, integer and Boolean indexing.
import numpy as np
# Create a 2D NumPy array
array_2d = np.array([[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]])
print("Original Array:")
print(array_2d)
# ---- Basic Slicing ----
# Slice: Get the first two rows, and the first three columns
sliced_array = array_2d[:2, :3]
print("\nBasic Slicing (first two rows, first three columns):")
print(sliced_array)
# Slice: Get every second row and every second column
sliced_step_array = array_2d[::2, ::2]
print("\nBasic Slicing with step (every second row, every second
column):")
print(sliced_step_array)
# ---- Integer Indexing ----
# Integer Indexing: Get the element at the second row and third
column (indexing starts at 0)
element = array_2d[1, 2]
print("\nInteger Indexing (Element at row 2, column 3):", element)
# Integer Indexing: Get multiple specific elements (2nd row, 1st
column and 4th row, 3rd column)
elements = array_2d[[1, 3], [0, 2]]
print("\nInteger Indexing (Elements at (2,1) and (4,3)):")
print(elements)
# ---- Boolean Indexing ----
# Boolean Indexing: Get all elements greater than 10
boolean_condition = array_2d > 10
print("\nBoolean Indexing (Elements greater than 10):")
print(array_2d[boolean_condition])
# Boolean Indexing: Get all elements in the first row where the
value is odd
odd_condition = array_2d[0] % 2 != 0
print("\nBoolean Indexing (Odd elements in the first row):")
print(array_2d[0, odd_condition])
OUTPUT:
Original Array:
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]
[13 14 15 16]]
Basic Slicing (first two rows, first three columns):
[[1 2 3]
[5 6 7]]
Basic Slicing with step (every second row, every second column):
[[ 1 3]
[ 9 11]]
Integer Indexing (Element at row 2, column 3): 7
Integer Indexing (Elements at (2,1) and (4,3)):
[ 5 15]
Boolean Indexing (Elements greater than 10):
[11 12 13 14 15 16]
Boolean Indexing (Odd elements in the first row):
[1 3]
5. Python program to find min, max, sum, cumulative sum of array
PROGRAM:
import numpy as np
# Create a NumPy array
array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
# Find the minimum value in the array
min_value = np.min(array)
print("Minimum value in the array:", min_value)
# Find the maximum value in the array
max_value = np.max(array)
print("Maximum value in the array:", max_value)
# Find the sum of all elements in the array
total_sum = np.sum(array)
print("Sum of all elements in the array:", total_sum)
# Find the cumulative sum of the array
cumulative_sum = np.cumsum(array)
print("Cumulative sum of the array:", cumulative_sum)
OUTPUT:
Minimum value in the array: 1
Maximum value in the array: 9
Sum of all elements in the array: 45
Cumulative sum of the array: [ 1 3 6 10 15 21 28 36 45]
6. Create a dictionary with at least five keys and each key represent value as a list
where this list contains at least ten values and convert this dictionary as a pandas
data frame and explore the data through the data frame as follows:
(a) Apply head () function to the pandas data frame
(b) Perform various data selection operations on Data Frame
PROGRAM:
import pandas as pd
# Create a dictionary with at least five keys, each containing a
list with at least ten values
data_dict = {
'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve', 'Frank',
'Grace', 'Hannah', 'Ivy', 'Jack'],
'Age': [23, 30, 35, 40, 28, 22, 33, 29, 26, 31],
'Salary': [50000, 60000, 70000, 80000, 55000, 60000, 75000,
45000, 65000, 70000],
'Department': ['HR', 'Engineering', 'Engineering', 'Marketing',
'HR', 'Sales', 'Marketing', 'Sales', 'Engineering', 'HR'],
'Location': ['New York', 'San Francisco', 'Los Angeles',
'Chicago', 'New York', 'San Francisco', 'Chicago', 'Los Angeles',
'San Francisco', 'New York']
}
# Convert the dictionary to a Pandas DataFrame
df = pd.DataFrame(data_dict)
# (a) Apply the head() function to the DataFrame
print("First 5 rows of the DataFrame using head():")
print(df.head())
# (b) Perform various data selection operations on the DataFrame
# Select a specific column (e.g., 'Name')
print("\nSelect 'Name' column:")
print(df['Name'])
# Select multiple columns (e.g., 'Name' and 'Salary')
print("\nSelect 'Name' and 'Salary' columns:")
print(df[['Name', 'Salary']])
# Select rows based on a condition (e.g., Age > 30)
print("\nSelect rows where Age is greater than 30:")
print(df[df['Age'] > 30])
# Select a specific row by index (e.g., row with index 3)
print("\nSelect the row with index 3:")
print(df.iloc[3])
# Select a specific cell (e.g., Salary of the person with index 2)
print("\nSelect the 'Salary' of the person with index 2:")
print(df.loc[2, 'Salary'])
# Select a specific range of rows (e.g., rows from index 2 to 5)
print("\nSelect rows from index 2 to 5:")
print(df.iloc[2:6])
# Select rows with specific columns (e.g., 'Name' and 'Age' for rows
3 to 7)
print("\nSelect 'Name' and 'Age' columns for rows from index 3 to
7:")
print(df.loc[3:7, ['Name', 'Age']])
OUTPUT:
First 5 rows of the DataFrame using head():
Name Age Salary Department Location
0 Alice 23 50000 HR New York
1 Bob 30 60000 Engineering San Francisco
2 Charlie 35 70000 Engineering Los Angeles
3 David 40 80000 Marketing Chicago
4 Eve 28 55000 HR New York
Select 'Name' column:
0 Alice
1 Bob
2 Charlie
3 David
4 Eve
5 Frank
6 Grace
7 Hannah
8 Ivy
9 Jack
Name: Name, dtype: object
Select 'Name' and 'Salary' columns:
Name Salary
0 Alice 50000
1 Bob 60000
2 Charlie 70000
3 David 80000
4 Eve 55000
5 Frank 60000
6 Grace 75000
7 Hannah 45000
8 Ivy 65000
9 Jack 70000
Select rows where Age is greater than 30:
Name Age Salary Department Location
2 Charlie 35 70000 Engineering Los Angeles
3 David 40 80000 Marketing Chicago
5 Frank 22 60000 Sales San Francisco
6 Grace 33 75000 Marketing Chicago
9 Jack 31 70000 HR New York
Select the row with index 3:
Name David
Age 40
Salary 80000
Department Marketing
Location Chicago
Name: 3, dtype: object
Select the 'Salary' of the person with index 2:
70000
Select rows from index 2 to 5:
Name Age Salary Department Location
2 Charlie 35 70000 Engineering Los Angeles
3 David 40 80000 Marketing Chicago
4 Eve 28 55000 HR New York
Select 'Name' and 'Age' columns for rows from index 3 to 7:
Name Age
3 David 40
4 Eve 28
5 Frank 22
6 Grace 33
7 Hannah 29
7. Select any two columns from the above data frame, and observe the change in
one attribute with respect to other attribute with scatter and plot operations in
matplotlib.
PROGRAM:
import matplotlib.pyplot as plt
import pandas as pd
# Create a dictionary with at least five keys, each containing a
list with at least ten values
data_dict = {
'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve', 'Frank',
'Grace', 'Hannah', 'Ivy', 'Jack'],
'Age': [23, 30, 35, 40, 28, 22, 33, 29, 26, 31],
'Salary': [50000, 60000, 70000, 80000, 55000, 60000, 75000,
45000, 65000, 70000],
'Department': ['HR', 'Engineering', 'Engineering', 'Marketing',
'HR', 'Sales', 'Marketing', 'Sales', 'Engineering', 'HR'],
'Location': ['New York', 'San Francisco', 'Los Angeles',
'Chicago', 'New York', 'San Francisco', 'Chicago', 'Los Angeles',
'San Francisco', 'New York']
}
# Convert the dictionary to a Pandas DataFrame
df = pd.DataFrame(data_dict)
# Select 'Salary' and 'Age' columns for plotting
salary = df['Salary']
age = df['Age']
# ---- Scatter Plot ----
plt.figure(figsize=(10, 5))
# Scatter plot to observe the relationship between Salary and Age
plt.subplot(1, 2, 1) # Create subplot (1 row, 2 columns, 1st plot)
plt.scatter(age, salary, color='blue', label='Salary vs Age',
edgecolor='black')
plt.title('Scatter Plot: Salary vs Age')
plt.xlabel('Age')
plt.ylabel('Salary')
plt.grid(True)
plt.legend()
# ---- Line Plot ----
plt.subplot(1, 2, 2) # Create subplot (1 row, 2 columns, 2nd plot)
plt.plot(age, salary, color='green', marker='o', linestyle='-',
label='Salary vs Age Trend')
plt.title('Line Plot: Salary vs Age')
plt.xlabel('Age')
plt.ylabel('Salary')
plt.grid(True)
plt.legend()
# Show both plots
plt.tight_layout()
plt.show()
OUTPUT: