Introduction to Data Science
Libraries
Introduction to NumPy
• NumPy stands for “Numerical Python”.
• It is a powerful Python library used for scientific computing, data
analysis, and numerical operations.
• It provides support for multi-dimensional arrays and efficient
mathematical functions.
• NumPy helps in handling large data and performing complex
calculations efficiently.
• It is widely used in Data Science, Machine Learning, and Artificial
Intelligence.
• Installation
• pip install numpy
• A NumPy array is a collection of elements of the same data type
(homogeneous data).
• It is stored in contiguous memory locations, which makes it faster and
more efficient than Python lists.
• It can store data in multiple dimensions such as 1D array (single list),
2D array (matrix), and 3D array (multiple matrices).
• It is designed for fast and efficient operations on large datasets.
• Types of Array
1. One Dimensional Array
2. Multi-Dimensional Array
• One Dimensional Array
• • A one-dimensional array is a linear array
• • It contains elements in a single row (one dimension)
• Multi-Dimensional Array
• • A multi-dimensional array contains data in more than one dimension
• • Data is stored in a tabular form (rows and columns)
• List vs Numpy Array
• All elements of an array are of the same data type.
• Elements of an array are stored in contiguous memory locations.
• Arrays are static and cannot be resized once they are created.
• NumPy array takes up less space in memory.
• Faster for numerical operations
• VS
• List can have elements of different data types.
• List elements are not stored contiguously in memory.
• Lists can be resized and modified easily.
• More space in memory.
• Slower for large data
Array Creation
• 1. Create 1D Array
• import numpy as np
• arr = [Link]([1, 2, 3, 4])
• print(arr)
• [1 2 3 4]
• 2. Create 2D Array
• import numpy as np
• arr = [Link]([[1, 2], [3, 4]])
• print(arr)
• Output:
• [[1 2]
[3 4]]
• 3. Create Array with Zeros
• import numpy as np
• array1 = [Link](4)
• print(array1)
• #output
• [[Link].]
• Create Array with Ones
• import numpy as np
• array1 = [Link](4)
• print(array1)
• #output
• [1. 1. 1. 1.]
• Create Range of Numbers
• import numpy as np
• arr = [Link](1, 5)
• print(arr)
• [1 2 3 4]
• 2. Array Manipulation
• (a)reshape
• arr = [Link]([1,2,3,4,5,6])
• new_arr = [Link](2,3)
• print(new_arr)
• [[1 2 3]
• [4 5 6]]
• B) Flatten
• arr = [Link]([[1,2],[3,4]])
• flat = [Link]()
• print(flat)
• [1 2 3 4]
• Array Indexing and Slicing
• NumPy allows powerful indexing and slicing operations on arrays, similar to
Python lists.
• Examples
• import numpyas np
• arr= [Link]([10, 20, 30, 40, 50])
• print(arr[2])
• #output :-30
• import numpyas np
• arr= [Link]([10, 20, 30, 40, 50])
• print(arr[1:4])
• #output:-[20 30 40]
Basic Operations on NumPy Array
• NumPy allows mathematical operations directly on arrays
• these operations are performed element-wise.
• import numpy as np
• arr= [Link]([1, 2, 3])
• arr2 = [Link]([4, 5, 6])
• print(arr+ arr2)
• print(arr* arr2)
• print(arr-arr2)
• print(arr/ arr2)
• Output
• [5 7 9]
• [ 4 10 18]
• [-3 -3 -3]
• [0.25 0.4 0.5 ]
Introduction to Pandas
• Definition
• Pandas is a Python library used for data analysis and data
manipulation
• It is used to work with structured data (tables)
• Widely used in:
• Data Science
• Machine Learning
• Introduction to DataFrame
• Definition
• A DataFrame is a 2D data structure (table format)
• It consists of:
• Rows
• Columns
• Similar to an Excel sheet or database table
• import pandas as pd
• data = {
• "Name": ["A", "B", "C"],
• "Age": [20, 21, 22]
•}
• df = [Link](data)
• print(df)
• Name Age
• 0 A 20
• 1 B 21
• 2 C 22
• Reading Data from CSV File
• Definition
• • Used to store tabular data in text format
• import pandas as pd
• df = pd.read_csv("[Link]")
• print(df)
• What is Data Cleaning?
• Definition:
• Data Cleaning is the process of identifying and fixing errors, and
missing values in data to improve its quality.
• Common Problems in Data:
1. Missing values (NaN)
2. Duplicate records
3. Invalid or wrong values
• 1. Handling Missing Values (NaN)
• Missing values are data points that are not available (represented as
• NaN in Pandas).
• Why it matters?
• Can give wrong results in calculations
• import pandas as pd
• data = {
• "Name": ["A", "B", "C", "D"],
• "Age": [20, None, 22, None],
• "Marks": [80, 90, None, 70]
•}
• df = [Link](data)
• print(df)
• Name Age Marks
• 0 A 20.0 80.0
• 1 B NaN 90.0
• 2 C 22.0 NaN
• 3 D NaN 70.0
• Check Missing Values
• [Link]()
• Drop Missing Values
• [Link]()
• Removes rows with any missing value
• Used when missing data is small
• Disadvantage: Data loss
• Fill Missing Values
• [Link](0)