Python Programming (SEP Scheme)
Lab Cycle Programs
PART-B
7B. Plot a line graph and a bar chart using Matplotlib
import [Link] as plt
# Sample data
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May']
sales = [120, 150, 170, 220, 300]
# Line Graph
[Link](figsize=(6, 4))
[Link](months, sales, color='blue', marker='o', linestyle='-', linewidth=2)
[Link]("Monthly Sales (Line Graph)")
[Link]("Month")
[Link]("Sales (in units)")
[Link](True)
[Link]()
# Bar Chart
[Link](figsize=(6, 4))
[Link](months, sales, color='orange')
[Link]("Monthly Sales (Bar Chart)")
[Link]("Month")
[Link]("Sales (in units)")
[Link]()
8B. Load a CSV file into Pandas and perform basic data analysis
import pandas as pd
# Create a small CSV file (if you don’t already have one)
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eva'],
'Age': [23, 35, 30, 40, 29],
'Salary': [45000, 54000, 50000, 62000, 58000]
}
df = [Link](data)
df.to_csv("[Link]", index=False)
# Load the CSV file
df = pd.read_csv("[Link]")
# Display basic analysis
print("=== Data from CSV ===")
print(df)
print("\n=== Basic Information ===")
print([Link]())
print("\n=== Summary Statistics ===")
print([Link]())
print("\n=== Average Salary ===")
print(df['Salary'].mean())
9B. Group a dataset by category and calculate summary statistics
import pandas as pd
# Sample dataset
data = {
'Department': ['HR', 'IT', 'Finance', 'IT', 'HR', 'Finance', 'IT'],
'Employee': ['A', 'B', 'C', 'D', 'E', 'F', 'G'],
'Salary': [40000, 55000, 60000, 52000, 42000, 58000, 61000]
}
df = [Link](data)
# Group by Department and calculate mean salary
grouped = [Link]('Department')['Salary'].mean().reset_index()
print("=== Original Data ===")
print(df)
print("\n=== Average Salary by Department ===")
print(grouped)
10B. Load a dataset from Scikit-learn and display its basic properties
from sklearn import datasets
import pandas as pd
# Load the famous Iris dataset
iris = datasets.load_iris()
# Display basic properties
print("=== Dataset Name: Iris ===")
print("Feature Names:", iris.feature_names)
print("Target Names:", iris.target_names)
print("Data Shape:", [Link])
# Convert to DataFrame for better view
df = [Link](data=[Link], columns=iris.feature_names)
df['target'] = [Link]
print("\n=== First 5 Rows of Dataset ===")
print([Link]())
print("\n=== Summary Statistics ===")
print([Link]())