Group A

Q2: Data Wrangling II Create an "Academic performance" dataset of students and perform the following operations using Python. 1. Scan all variables for missing values and inconsistencies. If there are missing values and/or inconsistencies, use any of the suitable techniques to deal with them. 2. Scan all numeric variables for outliers. If there are outliers, use any of the suitable techniques to deal with them. 3. Apply data transformations on at least one of the variables. The purpose of this transformation should be one of the following reasons: to change the scale for better understanding of the variable, to convert a non-linear relation into a linear one, or to decrease the skewness and convert the distribution into a normal distribution. Reason and document your approach properly.

Data Wrangling II

Solution and implementation for Q2 from Data Science Laboratory (ds).

2_data_wrangling_2.ipynb Download
Code Cell 1
# Import libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import zscore
from sklearn.preprocessing import MinMaxScaler
Code Cell 2
# Load dataset
df = pd.read_csv("data_2.csv")

print(df)
print(df.isnull().sum())
    Name   Age  GPA  Test_Score  Attendance
0   Amit  18.0  8.1          78        85.0
1   Neha  19.0  7.8          82        90.0
2  Rahul   NaN  9.0          91        88.0
3  Priya  20.0  8.5          85         NaN
4  Karan  21.0  7.2          76        76.0
5  Sneha  19.0  8.9          88        92.0
6  Rohit  18.0  3.5         620        87.0
7  Pooja  20.0  8.0          80        89.0
8  Arjun  50.0  9.5          92       900.0
9  Kavya  19.0  8.3          79        84.0
Name          0
Age           1
GPA           0
Test_Score    0
Attendance    1
dtype: int64
Code Cell 3
# Fill missing values
df['Age'] = df['Age'].fillna(df['Age'].mean())
df['Attendance'] = df['Attendance'].fillna(df['Attendance'].median())

print(df)
    Name        Age  GPA  Test_Score  Attendance
0   Amit  18.000000  8.1          78        85.0
1   Neha  19.000000  7.8          82        90.0
2  Rahul  22.666667  9.0          91        88.0
3  Priya  20.000000  8.5          85        88.0
4  Karan  21.000000  7.2          76        76.0
5  Sneha  19.000000  8.9          88        92.0
6  Rohit  18.000000  3.5         620        87.0
7  Pooja  20.000000  8.0          80        89.0
8  Arjun  50.000000  9.5          92       900.0
9  Kavya  19.000000  8.3          79        84.0
Code Cell 4
# Statistical summary
print(df.describe())
             Age        GPA  Test_Score  Attendance
count  10.000000  10.000000   10.000000   10.000000
mean   22.666667   7.880000  137.100000  167.900000
std     9.706813   1.673187  169.762874  257.270912
min    18.000000   3.500000   76.000000   76.000000
25%    19.000000   7.850000   79.250000   85.500000
50%    19.500000   8.200000   83.500000   88.000000
75%    20.750000   8.800000   90.250000   89.750000
max    50.000000   9.500000  620.000000  900.000000
Code Cell 5
# Boxplot for outliers
df[['Age','GPA','Test_Score','Attendance']].boxplot()

plt.show()
Cell 5 output
<Figure size 640x480 with 1 Axes>
Code Cell 6
# Remove outliers
z = np.abs(zscore(df[['Age','GPA','Test_Score','Attendance']]))

df = df[(z < 2.5).all(axis=1)]

print(df)
    Name        Age  GPA  Test_Score  Attendance
0   Amit  18.000000  8.1          78        85.0
1   Neha  19.000000  7.8          82        90.0
2  Rahul  22.666667  9.0          91        88.0
3  Priya  20.000000  8.5          85        88.0
4  Karan  21.000000  7.2          76        76.0
5  Sneha  19.000000  8.9          88        92.0
7  Pooja  20.000000  8.0          80        89.0
9  Kavya  19.000000  8.3          79        84.0
Code Cell 7
# Normalize data
scaler = MinMaxScaler()

df[['Age','GPA','Test_Score','Attendance']] = scaler.fit_transform(
    df[['Age','GPA','Test_Score','Attendance']]
)

print(df)
    Name       Age       GPA  Test_Score  Attendance
0   Amit  0.000000  0.500000    0.133333      0.5625
1   Neha  0.214286  0.333333    0.400000      0.8750
2  Rahul  1.000000  1.000000    1.000000      0.7500
3  Priya  0.428571  0.722222    0.600000      0.7500
4  Karan  0.642857  0.000000    0.000000      0.0000
5  Sneha  0.214286  0.944444    0.800000      1.0000
7  Pooja  0.428571  0.444444    0.266667      0.8125
9  Kavya  0.214286  0.611111    0.200000      0.5000
Code Cell 8
# Check skewness
print(df[['Age','GPA','Test_Score','Attendance']].skew())
Age           1.009749
GPA          -0.346064
Test_Score    0.597254
Attendance   -1.477838
dtype: float64
Data_2.csv Download
Name,Age,GPA,Test_Score,Attendance
Amit,18.0,8.1,78,85.0
Neha,19.0,7.8,82,90.0
Rahul,,9.0,91,88.0
Priya,20.0,8.5,85,
Karan,21.0,7.2,76,76.0
Sneha,19.0,8.9,88,92.0
Rohit,18.0,3.5,620,87.0
Pooja,20.0,8.0,80,89.0
Arjun,50.0,9.5,92,900.0
Kavya,19.0,8.3,79,84.0

Other Questions in Data Science Laboratory

See All Available Questions
Download