UNIT-V:
Introduction to Data Science: Functional Programming, JSON and XML in Python, NumPy
With Python, Pandas.
Sample Experiments:
1. Python program to check whether a JSON string contains complex object or not.
2. Python Program to demonstrate NumPy arrays creation using array () function.
3. Python program to demonstrate use of ndim, shape, size, dtype.
4. Python program to demonstrate basic slicing, integer and Boolean indexing.
5. Python program to find min, max, sum, cumulative sum of array
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
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
Experiment-1
Aim:
Write a Python program to check whether a JSON string contains a complex object or not.
Program
Import json
Def has_complex_object(json_str):
Try:
Obj = [Link](json_str)
For value in [Link]():
If isinstance(value, (dict, list)):
Return True
Return False
Except [Link]:
Return False
# Example
Json_str1 = ‘{“name”:”Alice”, “age”:25}’
Json_str2 = ‘{“name”:”Bob”, “address”:{“city”:”Delhi”,”pin”:110001}}’
Print(“JSON1 has complex object?”, has_complex_object(json_str1))
Print(“JSON2 has complex object?”, has_complex_object(json_str2))
Output
JSON1 has complex object? False
JSON2 has complex object? True
Experiment-2
Aim:
Write a Python program to demonstrate NumPy arrays creation using array() function.
Program
Import numpy as np
Arr = [Link]([1, 2, 3, 4, 5])
Print(“NumPy Array:”, arr)
Print(“Type:”, type(arr))
Output
NumPy Array: [1 2 3 4 5]
Type: <class ‘[Link]’>
Experiment-3
Aim:
Write a Python program to demonstrate use of ndim, shape, size, dtype.
Program
Import numpy as np
Arr = [Link]([[1, 2, 3], [4, 5, 6]])
Print(“Array:\n”, arr)
Print(“Dimensions (ndim):”, [Link])
Print(“Shape:”, [Link])
Print(“Size:”, [Link])
Print(“Data Type:”, [Link])
Output:
Array:
[[1 2 3]
[4 5 6]]
Dimensions (ndim): 2
Shape: (2, 3)
Size: 6
Data Type: int64
Experiment-4
Aim:
Write a Python program to demonstrate basic slicing, integer and Boolean indexing.
Program
Import numpy as np
Arr = [Link]([10, 20, 30, 40, 50])
# Slicing
Print(“Slice [1:4]:”, arr[1:4])
# Integer Indexing
Print(“Specific elements:”, arr[[0, 2, 4]])
# Boolean Indexing
Print(“Values > 25:”, Program[arr > 25])
Output:
Slice [1:4]: [20 30 40]
Specific elements: [10 30 50]
Values > 25: [30 40 50]
Experiment-5
Aim:
Write a Python program to find min, max, sum, cumulative sum of array.
Program
Import numpy as np
Arr = [Link]([5, 10, 15, 20])
Print(“Min:”, [Link]())
Print(“Max:”, [Link]())
Print(“Sum:”, [Link]())
Print(“Cumulative Sum:”, [Link]())
Output:
Min: 5
Max: 20
Sum: 50
Cumulative Sum: [ 5 15 30 50]
Experiment-6
Aim:
Create a dictionary with at least five keys and convert it into a pandas DataFrame. Explore
using head() and selection operations.
Program
Import pandas as pd
# Dictionary with lists
Data = {
“Math”: [45, 78, 89, 56, 90, 67, 88, 92, 76, 81],
“Science”: [56, 82, 75, 63, 95, 72, 89, 91, 84, 77],
“English”: [65, 70, 80, 55, 90, 60, 85, 92, 73, 88],
“History”: [55, 68, 78, 50, 85, 66, 80, 93, 74, 82],
“Computers”: [70, 88, 90, 65, 95, 75, 92, 97, 85, 89]
}
Df = [Link](data)
Print(“Head of DataFrame:\n”, [Link]())
Print(“\nSelect ‘Math’ column:\n”, df[“Math”])
Print(“\nSelect first 5 rows of ‘Science’:\n”, df[“Science”].head())
Print(“\nSelect multiple columns (English, Computers):\n”, df[[“English”, “Computers”]])
Output (sample):
Head of DataFrame:
Math Science English History Computers
0 45 56 65 55 70
1 78 82 70 68 88
2 89 75 80 78 90
3 56 63 55 50 65
4 90 95 90 85 95
Experiment-7
Aim:
Select any two columns from the DataFrame and plot scatter & line plots using Matplotlib.
Program
Import pandas as pd
Import [Link] as plt
# Using previous DataFrame ‘df’
[Link](df[“Math”], df[“Science”], color=’blue’)
[Link](“Math Marks”)
[Link](“Science Marks”)
[Link](“Scatter Plot: Math vs Science”)
[Link]()
# Line Plot
[Link](df[“Math”], df[“Science”], marker=’o’)
[Link](“Math Marks”)
[Link](“Science Marks”)
[Link](“Line Plot: Math vs Science”)
[Link]()
Output:
Scatter Plot between Math and Science.
Line Plot between Math and Science.