Class 1 - 2024 Business Analytics
Class 1 - 2024 Business Analytics
In [1]:
import numpy as np
1. Creating Arrays
In [2]:
# Creating an array from a list
arr1 = np.array([1, 2, 3, 4, 5])
print("Array from list:", arr1)
arr1[0]
In [3]:
# Creating an array with zeros
arr2 = np.zeros((3, 4))
print("Array of zeros:\n", arr2)
arr2[2,3]
Array of zeros:
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
0.0
Out[3]:
In [4]:
# Creating an array with ones
arr3 = np.ones((2, 3), dtype = int)
print("Array of ones:\n", arr3)
Array of ones:
[[1 1 1]
[1 1 1]]
In [5]:
# Creating an array with a range of numbers
arr4 = np.arange(10)
print("Array with range of numbers:", arr4)
In [6]:
# Creating an array with evenly spaced values
arr5 = np.linspace(0, 1, 5)
print("Array with evenly spaced values:", arr5)
# Indexing
print("Element at index 0:", arr[0])
print("Element at index 3:", arr[3])
In [8]:
# Slicing
print("Elements from index 1 to 3:", arr[1:4])
print("Elements from the start to index 3:", arr[:4])
print("Elements from index 2 to the end:", arr[2:])
3. Multi-dimensional Arrays
In [9]:
# Creating a 2D array
arr2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
In [10]:
print("Element at row 1, column 2:", arr2d[0, 1])
In [11]:
# Indexing
print("Element at row 1, column 2:", arr2d[0, 1])
In [12]:
# Slicing
print("First two rows and columns 1 and 2:\n", arr2d[:2, 0:2])
In [14]:
# Element-wise addition
arr1 + arr2
array([5, 7, 9])
Out[14]:
In [15]:
# Element-wise subtraction
print("Element-wise subtraction:", arr1 - arr2)
In [16]:
# Element-wise multiplication
print("Element-wise multiplication:", arr1 * arr2)
In [17]:
# Element-wise division
print("Element-wise division:", arr1 / arr2)
In [19]:
# Using universal functions
print("Sine of the array elements:", np.sin(arr))
print("Exponential of the array elements:", np.exp(arr))
print("Square root of the array elements:", np.sqrt(arr))
Reshaped 2D array:
[[1 2 3]
[4 5 6]
[7 8 9]]
Mean: 3.0
Standard Deviation: 1.4142135623730951
Introduction to Pandas
In [24]:
import pandas as pd
1. Creating DataFrames
In [25]:
# Creating a DataFrame from a dictionary
data = {
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'City': ['New York', 'Los Angeles', 'Chicago']
}
df = pd.DataFrame(data)
print("DataFrame from dictionary:\n", df)
In [28]:
print("DataFrame from CSV file:\n", df)
In [30]:
# Selecting multiple columns
print("Selecting 'Name' and 'Age' columns:\n", df[['Name', 'Age']])
In [31]:
# Selecting rows by index
print("Selecting first row:\n", df.iloc[0])
In [32]:
# Selecting rows by condition
print("Selecting rows where Age > 25:\n", df[df['Age'] > 25])
3. Modifying DataFrames
In [33]:
# Adding a new column
df['Salary'] = [70000, 80000, 90000]
print("DataFrame after adding 'Salary' column:\n", df)
In [34]:
# Modifying an existing column
df['Age'] = df['Age'] + 1
print("DataFrame after modifying 'Age' column:\n", df)
In [35]:
# Deleting a column
df = df.drop('City', axis=1)
print("DataFrame after deleting 'City' column:\n", df)
In [37]:
# Identifying missing values
print("Missing values in DataFrame:\n", df.isnull())
In [38]:
# Dropping rows with missing values
df_dropped = df.dropna()
print("DataFrame after dropping rows with missing values:\n", df_dropped)
In [39]:
# Filling missing values
df_filled = df.fillna({'Age': df['Age'].mean(), 'City': 'Unknown'})
print("DataFrame after filling missing values:\n", df_filled)
In [41]:
# Sorting the DataFrame by a column
sorted_df = df.sort_values(by='Age', ascending = False)
print("DataFrame sorted by 'Age' (descending):\n", sorted_df)
In [42]:
import matplotlib.pyplot as plt
ratings = ['Poor', 'Below Average', 'Average', 'Above Average', 'Excellent']
frequency = [2, 3, 5, 9, 1]
colors = ['blue', 'yellow', 'green', 'red', 'orange']
plt.bar(ratings, frequency, color=colors)
plt.xlabel('Rating')
plt.ylabel('Frequency')
plt.title('Rating Frequency Bar Graph')
plt.show()
In [ ]: