NumPy (Numerical Python)
What is NumPy?
NumPy is a Python library used for numerical computing. It provides:
Powerful N-dimensional arrays
Mathematical operations on arrays
Import NumPy
import numpy as np
Create 1D Array
arr = np.array([1, 2, 3, 4, 5])
print(arr)
Create 2D Array
arr2d = np.array([[1, 2], [3, 4]])
print(arr2d)
Array with Zeros
zeros = np.zeros((2, 3))
print(zeros)
Array with Ones
ones = np.ones((3, 2))
print(ones)
Random Array
rand = np.random.rand(2, 3)
print(rand)
Range of Numbers
r = np.arange(10, 20, 2)
print(r)
Reshape Array
a = np.arange(12)
reshaped = a.reshape(3, 4)
print(reshaped)
Array Indexing and Slicing
arr = np.array([10, 20, 30, 40, 50])
print(arr[1:4]) # Output: [20 30 40]
Array Arithmetic
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(a + b)
print(a * b)
Array Statistics
data = np.array([10, 20, 30, 40])
print("Mean:", np.mean(data))
print("Sum:", np.sum(data))
print("Std Dev:", np.std(data))
Sorting an Array
arr = np.array([10, 5, 2, 8])
print(np.sort(arr))
Generate Random Integers
rand_ints = np.random.randint(1, 10, size=(2, 3))
print(rand_ints)
What will be the output of following code- import numpy as np
A=np.array([24,46,57,14,68,34,89,92])
print(A[7:3:-1])
print(A[2:6])
Ans:
[92 89 34 68]
[57 14 68 34]
What will be the output of following code- import numpy as np
A=np.array([1,2,3,4,5,6,7,8,9,10,11,12]) print(A[10:5:-2])
Ans:
[11 9 7]
What will be the output of following code-
import numpy as np
A=np.ones(6) print(A)
B=np.reshape(A,(2,3)) print(B)
Ans:
[1. 1. 1. 1. 1. 1.]
[[1. 1. 1.]
[1. 1. 1.]]
What will be the output of following code-
import numpy as np
arr= np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
print(arr[::-2])
Ans:
[9 7 5 3 1]
What will be the output of following code-
import numpy as np
arr= np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
print(arr[-2::-2])
Ans [8 6 4 2 0]
What will be the output of following program:
import numpy as np
A=np.array([24,46,57,14,68,34,89,92])
print(A[-6:len(A)-1])
Ans: [57 14 68 34 89]
What will be the output of following program:
import numpy as np
A=np.array([24,46,57,14,68,34,89,92])
B=np.array([24,78,66,14,68,34,70,92])
c=np.where(A==B) print(c)
Ans: (array([0, 3, 4, 5, 7], dtype=int32),)
WAP to swap first two columns in a 2D numpy array?
Ans:
import numpy as np
arr = np.arange(9).reshape(3,3) print(arr)
print(arr[:, [1,0,2]])
Or
import numpy as np
arr = np.arange(9).reshape(3,3) print(arr)
arr[:, [0,1]]=arr[:,[1,0]]
print(arr)
Pandas in Python
Pandas is a powerful open-source library in Python used for:
Data analysis
Data manipulation
Reading and writing data (CSV, Excel, SQL, etc.)
Feature Description
Easy-to-use data structures Series, DataFrame
Data cleaning Handle missing values, filter, rename
File I/O Read/write CSV, Excel, JSON, SQL
Data analysis Aggregation, grouping, merging
Time-series support Powerful time series tools
1. Series
A one-dimensional labeled array.
import pandas as pd
s = pd.Series([10, 20, 30, 40])
print(s)
2. DataFrame
A 2D labeled data structure (like Excel table).
data = {
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35]
}
df = pd.DataFrame(data)
print(df)
Basic Information
df.head() # First 5 rows
df.tail() # Last 5 rows
df.shape # Rows and columns
df.info() # Data types and memory
df.describe()
Selecting Columns and Rows
df['Name'] # Select column
df[['Name', 'Age']] # Multiple columns
df.iloc[0] # First row by index
df.loc[0] # First row by label
Create a DataFrame
Q: Create a DataFrame with 3 columns: Name, Age, City
Solution:
import pandas as pd
data = {
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'City': ['New York', 'London', 'Paris']
}
df = pd.DataFrame(data)
print(df)
Create DataSeries:
import pandas as pd
s = pd.Series([2, 4, 6, 8, 10])
print(s)
Sample Output:
0 2
1 4
2 6
3 8
4 10
dtype: int64
Viewing/Inspecting Data
df.head(n) First n rows of the DataFrame
df.tail(n) Last n rows of the DataFrame
df.shape Number of rows and columns
df.info() Index, Datatype and Memory information
df.describe() Summary statistics for numerical columns
s.value_counts(dropna=False) View unique values and counts
df.apply(pd.Series.value_counts) Unique values and counts for all columns
Statistics
df.describe() Summary statistics for numerical columns
df.mean() Returns the mean of all columns
df.corr() Returns the correlation between columns in a DataFrame
df.count() Returns the number of non-null values in each DataFrame column
df.max() Returns the highest value in each column
df.min() Returns the lowest value in each column
df.median() Returns the median of each column
df.std() Returns the standard deviation of each column