05-Unit-V Python Lecture Notes
05-Unit-V Python Lecture Notes
Pandas: Pandas: (Data Analysis): Data Frame and Series, Data Frame operations, Data
Slicing, indexing, Data Frame functions, Reading the files-csv, excel.
1. Introduction to NumPy
Installation:
Importing NumPy:
import numpy as np
print(arr1, arr1.dtype)
print(arr2, arr2.dtype)
print(arr3, arr3.dtype)
Output:
[1 2 3] int32
[1.1 2.2 3.3] float64
[ True False True] bool
Creating Arrays:
Output:
[[1 2 3]
[4 5 6]]
Shape: (2, 3), Dimensions: 2
Example:
arr = np.array([10, 20, 30])
copy_arr = arr.copy() # Independent copy
view_arr = arr.view() # Just a view of original data
copy_arr[0] = 99
view_arr[1] = 88
Example:
print(arr1 + arr2) # [5 7 9]
print(arr1 * arr2) # [4 10 18]
print(arr1 ** 2) # [1 4 9]
Example:
print(arr[1]) # 20 (Indexing)
print(arr[1:4]) # [20 30 40] (Slicing)
print(arr[-1]) # 50 (Negative Indexing)
7. Splitting Arrays
8. Shape Manipulation
Example:
print(reshaped)
Output:
[[1 2 3]
[4 5 6]]
9. Stacking Arrays
Example:
Pandas is a Python library used for data manipulation and analysis. It provides two
main data structures:
1. Introduction to Pandas
Installation:
Importing Pandas:
import pandas as pd
Example:
# Creating a Series
s = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])
print(s)
# Creating a DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [24, 27, 22]}
df = pd.DataFrame(data)
print(df)
Output:
a 1
b 2
c 3
d 4
dtype: int64
Name Age
0 Alice 24
1 Bob 27
2 Charlie 22
3. DataFrame Operations
You can manipulate DataFrames using functions like head(), tail(), or describe().
Example:
You can select specific rows and columns using labels or positions.
Example:
5. DataFrame Functions
Example:
Pandas can read data from various formats, including CSV and Excel.
df = pd.read_csv('data.csv')
print(df.head())
df = pd.read_excel('data.xlsx')
print(df.head())
Summary
1. What is NumPy?
Answer:
NumPy (Numerical Python) is a Python library used for scientific computing. It
provides support for multi-dimensional arrays and mathematical operations on
these arrays, such as linear algebra, statistical operations, and element-wise
operations. It is faster than traditional Python lists due to its optimized C-based
implementation.
Answer:
A Pandas DataFrame is a 2-dimensional, tabular data structure with labeled axes
(rows and columns). It is similar to a spreadsheet or SQL table and is useful for
working with structured data.
Answer:
ndarray (N-dimensional array) is the core data structure in NumPy. It can hold
multiple elements of the same data type across various dimensions (1D, 2D, or
more). Operations on these arrays are performed element-wise and efficiently.
Answer:
You can read a CSV file using the read_csv() function from the Pandas library:
import pandas as pd
data = pd.read_csv('filename.csv')
Answer:
• View: A view refers to shared data; changes in the original array reflect in
the view.
• Copy: A copy creates a new array independent of the original; changes in
one do not affect the other.
5 Marks Questions (Explanation and Short Code Questions)
1. Explain the difference between NumPy arrays and Python lists with an
example.
Answer:
• Python Lists: Can hold elements of different data types, but they are slower
and occupy more memory.
• NumPy Arrays: Store elements of the same data type. Operations are faster
because of better memory management.
Example:
import numpy as np
# NumPy array
arr = np.array([1, 2, 3])
# Python list
lst = [1, 2, 3]
arr + 2 # Output: [3 4 5]
Answer:
A Pandas Series is a one-dimensional labeled array capable of holding any data
type. It can act like a list or dictionary.
Code Example:
import pandas as pd
Output:
a 10
b 20
c 30
dtype: int64
Answer:
import numpy as np
# Slice to get the first two rows and first two columns
sliced_arr = arr[:2, :2]
print(sliced_arr)
Output:
[[1 2]
[4 5]]
Answer:
You can change the shape using the reshape() method.
import numpy as np
Output:
[[1 2 3]
[4 5 6]]
Answer: You can index a DataFrame using row and column labels.
import pandas as pd
Output:
0 Alice
1 Bob
Name: Name, dtype: object
Name Bob
Age 27
Name: 1, dtype: object
Answer:
NumPy allows element-wise arithmetic operations such as addition, subtraction,
multiplication, and division.
Example:
import numpy as np
# Addition
print(arr1 + arr2)
# Multiplication
print(arr1 * arr2)
# Scalar addition
print(arr1 + 10)
Output:
[5 7 9]
[ 4 10 18]
[11 12 13]
Answer:
import numpy as np
Output:
3. How can you read an Excel file using Pandas? Write a code example.
Answer:
import pandas as pd
This will read the Excel file and print the first 5 rows using head().
15 Marks Questions (In-depth Questions Covering Concepts and Code)
1. Explain how you can stack NumPy arrays and manipulate their shapes.
Provide examples.
Answer:
Stacking means combining multiple arrays along a particular axis.
Code Example:
import numpy as np
Output:
Stacked Rows:
[[1 2 3]
[4 5 6]]
Stacked Columns:
[[1 4]
[2 5]
[3 6]]
Answer:
import pandas as pd
# (1) Creating a DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'],
'Age': [24, 27, 22, 32],
'City': ['NY', 'LA', 'NY', 'LA']}
df = pd.DataFrame(data)
Output:
Filtered Data:
Name Age City
1 Bob 27 LA
3 David 32 LA