EX. N O: 1 (a) DATE: CREATI N G A N D ACCESSI N G ELEME N TS OF A N UMPY 2D ARRAY USI N G I N DEXI N G A N D SLICI N G AIM: To write a Python program to generate a 2D array using NumPy and access its elements using forward indexing, backward indexing, and perform slicing. SOURCE CODE: import numpy as np L = np.array([[10, 20, 30], [40, 50, 60], [70, 80, 90]]) print("Original 2D Array:") print(L) print("\nForward Indexing [1][2] (Element at 2nd row, 3rd column):", L[1] [2]) print("\nBackward Indexing [-1][-1] (Last element):", L[-1][-1]) print("\nSlicing [0:2, 0:2] (Top-left 2x2 block):") print(L[0:2, 0:2]) print("\nSlicing second row [1, :]:") print(L[1, :]) print("\nSlicing third column [:, 2]:") print(L[:, 2]) EX. N O: 1 (b) DATE: PERFORMI N G ELEME N T-WISE ARITHMETIC OPERATIO N S O N 2D N UMPY ARRAYS AIM: To write a Python program to perform element-wise addition, subtraction, multiplication, and division on two 2D NumPy arrays of the same shape. SOURCE CODE: import numpy as np A = np.array([[10, 20, 30], [40, 50, 60]]) B = np.array([[1, 2, 3], [4, 5, 6]]) print("Array A:") print(A) print("\nArray B:") print(B) W = A + B print("\nElement-wise Addition (A + B):") print(W) X = A - B print("\nElement-wise Subtraction (A - B):") print(X) Y = A * B print("\nElement-wise Multiplication (A * B):") print(Y) Z = A / B print("\nElement-wise Division (A / B):") print(Z) RESULT: Thus, the above Python program has been executed and the output is verified successfully. EX. N O: 2 DATE: CREATI N G A PA N DAS SERIES USI N G A DICTIO N ARY A N D ACCESSI N G ELEME N TS WITH I N DEXI N G A N D SLICI N G AIM: To write a Python program to create a Pandas Series using a dictionary and access its elements using indexing and slicing. SOURCE CODE: import pandas as pd D = {'Eng': 90, 'Math': 85, 'Phy': 88, 'Che': 76, 'CS': 95, 'AI': 100} S = pd.Series(D) print("Pandas Series:\n", S) print("\nMarks in Maths:", S['Math']) print("\nLast element (negative indexing):", S[-1]) print("\nMarks in CS and AI:") print(S[['CS', 'AI']]) print("\nFirst 3 subjects (slicing):") print(S[:3]) EX.NO: 3 DATE: CREATI N G A DATAFRAME FOR EMPLOYEE DETAILS USI N G A DICTIO N ARY OF LISTS A N D ACCESSI N G RECORDS AIM: To write a Python program to create a Pandas DataFrame for employee details ( Emp_ID, N ame, Department, Salary ) using a dictionary of lists, and perform the following operations: (a) Display the entire DataFrame. (b) Display the first 5 records. (c) Display the last 3 records. SOURCE CODE: import pandas as pd Emp = { 'Emp_ID': [101, 102, 103, 104, 105, 106, 107], 'Name': ['Arun', 'Bhavya', 'Charan', 'Divya', 'Esha', 'Farhan', 'Geeta'], 'Department': ['HR', 'Finance', 'IT', 'IT', 'HR', 'Finance', 'Sales'], 'Salary': [50000, 55000, 60000, 58000, 52000, 59000, 48000] } df = pd.DataFrame(Emp) print("Entire DataFrame:") print(df) print("\nFirst 5 Records:") print(df.head(5)) print("\nLast 3 Records:") print(df.tail(3)) RESULT: Thus, the above Python program has been executed and the output is verified successfully. EX.NO: 4 DATE: CREATI N G A PA N DAS DATAFRAME USI N G SERIES FOR PATIE N T HEALTH PARAMETERS A N D ACCESSI N G ROWS A N D COLUM N S AIM: To write a Python program to create a Pandas DataFrame named df using Series for the given table of health parameters ( RBC, WBC, Platelets, Hemoglobin ) patient-wise and perform the following operations: (i) Display only the column 'RBC' from DataFrame df (ii) Display the row details of 'Pa ? ent1' and 'Pa ? ent2' (iii) Display the columns 'WBC' and 'Hemoglobin' for 'Pa ? ent2' and 'Pa ? ent3' (iv) Display any three consecutive rows and three columns from DataFrame df SOURCE CODE: import pandas as pd rbc = pd.Series([4.5, 4.9, 5.2], index=['Patient1', 'Patient2', 'Patient3']) wbc = pd.Series([7000, 7500, 6800], index=['Patient1', 'Patient2', 'Patient3']) platelets = pd.Series([250000, 260000, 245000], index=['Patient1', 'Patient2', 'Patient3']) hemoglobin = pd.Series([13.5, 14.2, 13.8], index=['Patient1', 'Patient2', 'Patient3']) df = pd.DataFrame({'RBC': rbc, 'WBC': wbc, 'Platelets': platelets, 'Hemoglobin': hemoglobin}) print("Original Data Frame:") print(df) print("\n(i) Column 'RBC':") print(df['RBC']) print("\n(ii) Rows 'Patient1' and 'Patient2':") print(df.loc[['Patient1', 'Patient2']]) print("\n(iii) Columns 'WBC' and 'Hemoglobin' for 'Patient2' and 'Patient3':") print(df.loc[['Patient2', 'Patient3'], ['WBC', 'Hemoglobin']]) print("\n(iv) Any 3 consecutive rows and 3 columns:") print(df.iloc[0:3, 0:3]) EX.NO: 5 DATE: CREATI N G A DATAFRAME FOR PLA N T GROWTH PROFILE A N D PERFORMI N G COLUM N I N SERTIO N , ROW ADDITIO N , A N D ROW DELETIO N AIM: To write a Python program to create a Pandas DataFrame using a dictionary of lists for the plant growth profile ( Light Intensity, CO₂ Level, Water Volume, Growth Rate ), and perform the following operations: (i) Add a new column "Soil pH" with values [6.5, 7.0, 6.8, 7.2] (ii) Add a new row "Tulsi" with values [250, 380, 950, 18.4, 6.9] (iii) Delete the row details of "Neem" from DataFrame df SOURCE CODE: import pandas as pd data = { 'Light Intensity': [300, 450, 500, 400], 'CO2 Level': [350, 400, 370, 390], 'Water Volume': [1000, 1200, 1100, 1050], 'Growth Rate': [15.2, 17.5, 16.1, 14.8] } df = pd.DataFrame(data, index=['Rose', 'Neem', 'Mango', 'Aloe Vera']) print("Original DataFrame:\n") print(df) df['Soil pH'] = [6.5, 7.0, 6.8, 7.2] print("\nDataFrame after adding new column 'Soil pH':\n") print(df) df.loc['Tulsi', :] = [250, 380, 950, 18.4, 6.9] print("\nDataFrame after adding new row 'Tulsi':\n") print(df) df.drop(index='Neem', inplace=True) print("\nDataFrame after deleting row 'Neem':\n") print(df) EX. N O: 7 DATE: CREATI N G A PYTHO N PROGRAM TO PERFORM WRITI N G A N D READI N G OPERATIO N S I N A CSV FILE AIM: To write a Python program that stores employee details such as Empno, Name, Salary and Department into an "Employee.csv" file, and also reads employee details from the same CSV file. SOURCE CODE: import pandas as pd Emp = { 'Emp.No': [101, 104, 502], 'Name': ['Anu', 'Raj', 'Uma'], 'Salary': [25000, 30000, 28000], 'Department': ['HR', 'IT', 'Finance'] } DF = pd.DataFrame(Emp, index=['E1', 'E2', 'E3']) print("\nOriginal Dataframe") print(DF) DF.to_csv("E:\\AI\\Employee.csv", index=False) print("\nThe above DataFrame stored successfully") S = pd.read_csv("E:\\AI\\Employee.csv") print("\nReading Data from Employee.csv file\n") print(S) EX. N O: 8 DATE: A N ALYZI N G STUDE N T MARKS FROM A PUBLIC CSV DATASET USI N G PA N DAS WITH STATISTICAL A N ALYSIS A N D MISSI N G VALUE HA N DLI N G AIM: To write a Python program that reads a student marks dataset (downloaded from a public source like data.gov.in or kaggle.com), handles missing values, and performs statistical analysis using Pandas to find mean, median, mode, and standard deviation. SOURCE CODE: import pandas as pd df = pd.read_csv('studentmarks.csv') print("\nOriginal Data Frame") print(df) df['Maths'].fillna(df['Maths'].mean(), inplace=True) df['Physics'].fillna(df['Physics'].mean(), inplace=True) df['Biology'].fillna(df['Biology'].mean(), inplace=True) print("\nUpdated Student Marks (After filling Missing values):") print(df) print("\nSubjects available: Maths, Physics, Biology") subject = input("\nEnter the subject you want to analyze: ") if subject in ['Maths', 'Physics', 'Biology']: print('\n') print(subject, "Statistics:") print("Mean:", df[subject].mean()) print("Median:", df[subject].median()) print("Mode:", df[subject].mode()[0]) print("Standard Deviation:", df[subject].std()) else: print("Invalid subject. Please enter Maths, Physics, or Biology.") Sample CSV file: RollNo,Name,Maths,Physics,Biology 101,Anu,85,78,90 102,Arun,80,85,88 103,Bala,75,,86 104,Deepa,,72,84 105,Ezhil,90,88,92 106,Farhan,82,80, EX. N O: 9 DATE: CREATI N G PYTHO N PROGRAM TO EVALUATE A STUDE N T EXAM RESULT PREDICTIO N MODEL USI N G ACCURACY, PRECISIO N , RECALL, F1-SCORE AIM: To write a Python program that builds a classification model to predict student exam results (Pass/Fail), and evaluates the model using accuracy, precision, recall, and F1-score. SOURCE CODE: import pandas as pd from sklearn.model_selection import train_test_split from sklearn.linear_model import LogisticRegression from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score df = pd.read_csv("StudentExamResults.csv") # Step 1: Handle missing values df = df.fillna(0) # Step 2: Change 'Pass' to 1 and 'Fail' to 0 so the computer can understand df['Result'] = df['Result'].replace({'Pass': 1, 'Fail': 0}) # Step 3: Select input features (X) and output (y) X = df[['Maths', 'Physics', 'Biology']] y = df['Result'] # Step 4: Split into train and test sets X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0) # Step 5: Train model model = LogisticRegression() model.fit(X_train, y_train) # Step 6: Predict on test data y_pred = model.predict(X_test) # Step 7: Evaluate model print("Model Evaluation Metrics:") print("Accuracy:", accuracy_score(y_test, y_pred)) print("Precision:", precision_score(y_test, y_pred)) print("Recall:", recall_score(y_test, y_pred)) print("F1 Score:", f1_score(y_test, y_pred)) Sample Dataset (StudentExamResults.csv): Name,Maths,Physics,Biology,Result Aarav,85,80,78,Pass Sneha,45,42,40,Fail Ishaan,90,88,85,Pass Tanya,35,30,32,Fail Rohan,78,75,72,Pass Fatima,40,38,35,Fail Neha,82,79,77,Pass Kunal,50,48,45,Fail Divya,88,84,82,Pass Mani,60,58,55,Fail EX.NO: 6 DATE: CREATING A PYTHON PROGRAM TO ACCESS AND DISPLAY DATAFRAME ATTRIBUTES AIM: To write a Python program to create a Pandas DataFrame and display its various attributes such as shape, size, data types, index, column labels, and to perform the transpose of the DataFrame. SOURCE CODE: import pandas as pd data = { 'Name': ['Arun', 'Bhavya', 'Uma'], 'Age': [25, 30, 28], 'Department': ['HR', 'IT', 'Finance'] } df = pd.DataFrame(data) print("Original DataFrame:\n", df) print("\nShape of DataFrame:", df.shape) print("Size of DataFrame:", df.size) print("\nData Types:\n", df.dtypes) print("\nIndex of DataFrame:", df.index) print("Column Labels:", df.columns) print("\nTranspose of DataFrame:\n", df.T) RESULT: Thus, the above Python program has been executed and the output is verified successfully.