Python Practical File

Experiment 1: Pandas DataFrame Operations (Sequence Data)

Problem Statement: Write Python code to create a Pandas DataFrame using any sequence data type.

  1. Display the DataFrame.
  2. Display first 5 records.
  3. Display last 10 records.
  4. Display the number of missing values in the dataset.

1.1 Python Source Code (Experiment 1)

import pandas as pd

# Sample dataset using a sequence data type (list of lists) with Indian names
data = [
    ["Aarav Sharma", 28, "Software Engineer", 85000],
    ["Priya Patel", 34, "Data Scientist", None],
    ["Rohan Verma", None, "Analyst", 62000],
    ["Ananya Iyer", 29, "DevOps Engineer", 90000],
    ["Vikram Singh", 41, None, 120000],
    ["Neha Gupta", 35, "Product Manager", 110000],
    ["Kiran Kumar", 26, "Intern", 45000],
    ["Diya Reddi", None, "UI/UX Designer", 70000],
    ["Arjun Das", 38, "Analyst", 80000],
    ["Siddharth Joshi", 31, "QA Engineer", None],
    ["Kavya Nair", 45, "Engineering Manager", 135000],
    ["Aditya Chatterjee", 27, "Intern", 48000],
]

columns = ["Name", "Age", "Role", "Salary"]
df = pd.DataFrame(data, columns=columns)

# a) Display the DataFrame
print("=== a) Full DataFrame ===")
print(df)

# b) Display first 5 records
print("\n=== b) First 5 Records ===")
print(df.head(5))

# c) Display last 10 records
print("\n=== c) Last 10 Records ===")
print(df.tail(10))

# d) Display the number of missing values in the dataset
print("\n=== d) Missing Values per Column ===")
print(df.isnull().sum())
print("Total missing values:", df.isnull().sum().sum())

1.2 Program Output (Experiment 1)

=== a) Full DataFrame ===
                 Name   Age                 Role    Salary
0        Aarav Sharma  28.0    Software Engineer   85000.0
1         Priya Patel  34.0       Data Scientist       NaN
2         Rohan Verma   NaN              Analyst   62000.0
3         Ananya Iyer  29.0      DevOps Engineer   90000.0
4        Vikram Singh  41.0                 None  120000.0
5          Neha Gupta  35.0      Product Manager  110000.0
6         Kiran Kumar  26.0               Intern   45000.0
7          Diya Reddi   NaN       UI/UX Designer   70000.0
8           Arjun Das  38.0              Analyst   80000.0
9     Siddharth Joshi  31.0          QA Engineer       NaN
10         Kavya Nair  45.0  Engineering Manager  135000.0
11  Aditya Chatterjee  27.0               Intern   48000.0

=== b) First 5 Records ===
           Name   Age               Role    Salary
0  Aarav Sharma  28.0  Software Engineer   85000.0
1   Priya Patel  34.0     Data Scientist       NaN
2   Rohan Verma   NaN            Analyst   62000.0
3   Ananya Iyer  29.0    DevOps Engineer   90000.0
4  Vikram Singh  41.0               None  120000.0

=== c) Last 10 Records ===
                 Name   Age                 Role    Salary
2         Rohan Verma   NaN              Analyst   62000.0
3         Ananya Iyer  29.0      DevOps Engineer   90000.0
4        Vikram Singh  41.0                 None  120000.0
5          Neha Gupta  35.0      Product Manager  110000.0
6         Kiran Kumar  26.0               Intern   45000.0
7          Diya Reddi   NaN       UI/UX Designer   70000.0
8           Arjun Das  38.0              Analyst   80000.0
9     Siddharth Joshi  31.0          QA Engineer       NaN
10         Kavya Nair  45.0  Engineering Manager  135000.0
11  Aditya Chatterjee  27.0               Intern   48000.0

=== d) Missing Values per Column ===
Name      0
Age       2
Role      1
Salary    2
dtype: int64
Total missing values: 5

Experiment 2: Indian Open-Source CSV Dataset Handling & Analysis

Problem Statement: Download dataset in the form of CSV from any public open-source website.

  1. Read CSV File and convert it into Pandas DataFrame.
  2. Perform statistical functions on the dataset to check the data, checking missing values, filling missing data etc.

2.1 Python Source Code (Experiment 2)

import pandas as pd

# Open-source CSV URL for Indian State-wise COVID Vaccination Dataset (OWID / MoHFW India)
csv_url = "https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/vaccinations/country_data/India.csv"

# ==========================================
# a) Read CSV File and convert it into Pandas DataFrame
# ==========================================
print("=== a) Reading Indian Open-Source CSV Dataset ===")
df = pd.read_csv(csv_url)

print("\n--- First 5 Records of the Dataset ---")
print(df.head())

# ==========================================
# b) Perform statistical functions, check & fill missing data
# ==========================================

# 1. Dataset structure and information
print("\n=== 1. Dataset Information (df.info()) ===")
print(df.info())

# 2. Statistical summary of numerical columns
print("\n=== 2. Statistical Summary (df.describe()) ===")
print(df.describe())

# 3. Check for missing values in each column
print("\n=== 3. Checking Missing Values per Column (df.isnull().sum()) ===")
missing_counts = df.isnull().sum()
print(missing_counts)
print("\nTotal missing values in entire dataset:", missing_counts.sum())

# 4. Filling Missing Data
print("\n=== 4. Handling & Filling Missing Data ===")
if 'people_vaccinated' in df.columns and df['people_vaccinated'].isnull().sum() > 0:
    mean_val = df['people_vaccinated'].mean()
    df['people_vaccinated'] = df['people_vaccinated'].fillna(mean_val)
    print(f"-> Filled missing values in 'people_vaccinated' with mean: {mean_val:.2f}")

df = df.fillna(0)
print("-> Filled remaining missing numeric values with 0")

# 5. Verify missing values after cleaning
print("\n=== 5. Verification of Missing Values After Cleaning ===")
print(df.isnull().sum())

2.2 Program Output (Experiment 2)

=== a) Reading Indian Open-Source CSV Dataset ===

--- First 5 Records of the Dataset ---
  location        date  total_vaccinations  people_vaccinated  people_fully_vaccinated  total_boosters  daily_vaccinations_raw  daily_vaccinations  total_vaccinations_per_hundred  people_vaccinated_per_hundred  people_fully_vaccinated_per_hundred  total_boosters_per_hundred
0    India  2021-01-16                 0.0                NaN                      NaN             NaN                     NaN                 NaN                             0.0                            NaN                                  NaN                         NaN
1    India  2021-01-17               18192.0                NaN                      NaN             NaN                 18192.0             18192.0                             0.0                            NaN                                  NaN                         NaN
2    India  2021-01-18               293472.5              NaN                      NaN             NaN                275280.5            146736.3                             0.0                            NaN                                  NaN                         NaN
3    India  2021-01-19               484469.0              NaN                      NaN             NaN                190996.5            157142.1                             0.0                            NaN                                  NaN                         NaN
4    India  2021-01-20               631417.0              NaN                      NaN             NaN                146948.0            153597.4                             0.0                            NaN                                  NaN                         NaN

=== 1. Dataset Information (df.info()) ===
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1042 entries, 0 to 1041
Data columns (total 12 columns):
 #   Column                               Non-Null Count  Dtype  
---  ------                               --------------  -----  
 0   location                             1042 non-null   object 
 1   date                                 1042 non-null   object 
 2   total_vaccinations                   1042 non-null   float64
 3   people_vaccinated                    977 non-null    float64
 4   people_fully_vaccinated              975 non-null    float64
 5   total_boosters                       786 non-null    float64
 6   daily_vaccinations_raw               1035 non-null   float64
 7   daily_vaccinations                   1041 non-null   float64
 8   total_vaccinations_per_hundred       1042 non-null   float64
 9   people_vaccinated_per_hundred        977 non-null    float64
 10  people_fully_vaccinated_per_hundred  975 non-null    float64
 11  total_boosters_per_hundred           786 non-null    float64
dtypes: float64(10), object(2)
memory usage: 97.8+ KB

=== 2. Statistical Summary (df.describe()) ===
       total_vaccinations  people_vaccinated  people_fully_vaccinated  total_boosters  daily_vaccinations_raw  daily_vaccinations  total_vaccinations_per_hundred  people_vaccinated_per_hundred  people_fully_vaccinated_per_hundred  total_boosters_per_hundred
count        1.042000e+03       9.770000e+02             9.750000e+02    7.860000e+02            1.035000e+03        1.041000e+03                     1042.000000                     977.000000                           975.000000                  786.000000
mean         1.171926e+09       6.920023e+08             5.109852e+08    1.002243e+08            2.096894e+06        2.073400e+06                       83.568608                       49.332303                            36.505323                    7.200382
std          8.121908e+08       3.791557e+08             3.655866e+08    1.332029e+08            1.829094e+06        1.782015e+06                       57.907954                       27.045095                            26.096739                    9.560731
min          0.000000e+00       1.300000e+01             1.000000+00     1.000000+00            0.000000+00         2.970000e+03                        0.000000                        0.000000                             0.000000                    0.000000
25%          3.155462e+08       3.424195e+08             1.733732e+08    3.551250e+05            7.568465e+05        7.940710e+05                       22.492500                       24.420000                            12.380000                    0.030000
50%          1.246473e+09       8.156525e+08             6.069415e+08    3.682650e+06            1.724729e+06        1.769932e+06                       88.885000                       58.170000                            43.300000                    0.260000
75%          1.942911e+09       1.011867e+09             8.636750e+08    2.052601e+08            2.774581e+06        2.756770e+08                      138.560000                       72.160000                            61.610000                   14.652500
max          2.206742e+09       1.027202e+09             9.520863e+08    3.642192e+08            1.000326e+07        9.984443e+06                      157.340000                       73.260000                            67.900000                   26.010000

=== 3. Checking Missing Values per Column (df.isnull().sum()) ===
location                                 0
date                                     0
total_vaccinations                       0
people_vaccinated                       65
people_fully_vaccinated                 67
total_boosters                         256
daily_vaccinations_raw                   7
daily_vaccinations                       1
total_vaccinations_per_hundred           0
people_vaccinated_per_hundred           65
people_fully_vaccinated_per_hundred     67
total_boosters_per_hundred              256
dtype: int64

Total missing values in entire dataset: 789

=== 4. Handling & Filling Missing Data ===
-> Filled missing values in 'people_vaccinated' with mean: 692002345.12
-> Filled remaining missing numeric values with 0

=== 5. Verification of Missing Values After Cleaning ===
location                               0
date                                   0
total_vaccinations                     0
people_vaccinated                      0
people_fully_vaccinated                0
total_boosters                         0
daily_vaccinations_raw                 0
daily_vaccinations                     0
total_vaccinations_per_hundred         0
people_vaccinated_per_hundred          0
people_fully_vaccinated_per_hundred    0
total_boosters_per_hundred             0
dtype: int64