Python Programming Lab Record
Program 1: Create DataFrame & Handle Missing Values
Aim
To create a DataFrame from a dictionary and handle missing values.
Algorithm
Import pandas and numpy libraries.
Create a dictionary containing missing values.
Convert dictionary into a DataFrame.
Display the DataFrame.
Handle missing values using fillna().
Display updated DataFrame.
Stop.
Program
import pandas as pd
import numpy as np
data = {
"Name": ["Arun", "Bala", "Charan"],
"Age": [20, [Link], 22],
"Marks": [85, 90, [Link]]
}
df = [Link](data)
print("Original DataFrame:")
print(df)
[Link](0, inplace=True)
print("\nAfter Handling Missing Values:")
print(df)
Output
Original DataFrame:
Name Age Marks
0 Arun 20.0 85.0
1 Bala NaN 90.0
2 Charan 22.0 NaN
After Handling Missing Values:
Name Age Marks
0 Arun 20.0 85.0
1 Bala 0.0 90.0
2 Charan 22.0 0.0
Result
Thus, a DataFrame was created and missing values were handled successfully.
Program 2: DataFrame Statistics and Add New Column
Aim
To calculate basic statistics of a DataFrame column and add a new column based on existing
columns.
Algorithm
Import pandas library.
Create a DataFrame.
Calculate statistics using describe().
Add new column using existing columns.
Display updated DataFrame.
Stop.
Program
import pandas as pd
data = {
"Name": ["Arun", "Bala", "Charan"],
"Math": [80, 75, 90],
"Science": [85, 70, 95]
}
df = [Link](data)
print("Statistics:")
print(df["Math"].describe())
df["Total"] = df["Math"] + df["Science"]
print("\nUpdated DataFrame:")
print(df)
Output
Statistics:
count 3.0
mean 81.6
std ...
min 75.0
max 90.0
Updated DataFrame:
Name Math Science Total
0 Arun 80 85 165
1 Bala 75 70 145
2 Charan 90 95 185
Result
Thus, statistics were calculated and a new column was added successfully.
Program 3: NumPy 2D Array Operations
Aim
To create a 2D array, perform basic operations, and calculate mean and standard deviation.
Algorithm
Import numpy library.
Create a 2D array.
Perform arithmetic operations.
Calculate mean and standard deviation.
Display results.
Stop.
Program
import numpy as np
arr = [Link]([[1, 2, 3],
[4, 5, 6]])
print("Array:")
print(arr)
print("\nAddition:")
print(arr + 2)
print("\nMultiplication:")
print(arr * 2)
print("\nMean:", [Link](arr))
print("Standard Deviation:", [Link](arr))
Output
Array:
[[1 2 3]
[4 5 6]]
Addition:
[[3 4 5]
[6 7 8]]
Multiplication:
[[ 2 4 6]
[ 8 10 12]]
Mean: 3.5
Standard Deviation: 1.707
Result
Thus, 2D array operations and statistical calculations were performed successfully.
Program 4: Load CSV and Explore Dataset
Aim
To load a CSV file, display first few records, and find total entries.
Algorithm
Import pandas library.
Load CSV file using read_csv().
Display first few records using head().
Find total entries using len().
Display results.
Stop.
Program
import pandas as pd
df = pd.read_csv("[Link]")
print("First Five Records:")
print([Link]())
print("\nTotal Entries:", len(df))
Output
First Five Records:
ID Name Marks
0 1 Arun 85
1 2 Bala 90
2 3 Charan 88
Total Entries: 50
Result
Thus, CSV data was loaded, explored, and total entries were identified successfully.