Program 1
import pandas as pd
df = pd.read_csv('dav_lab1.csv')
df
[Link]()
[Link]()
missing_occupation = df['Occupation'].isnull().sum()
missing_satisfaction = df['Satisfaction_Level'].isnull().sum()
print("Number of missing values in 'Occupation'", missing_occupation)
print("Number of missing values in 'Satisfaction_Level'", missing_satisfaction)
filled_occupation = df['Occupation'].notnull().sum()
filled_satisfaction = df['Satisfaction_Level'].notnull().sum()
print("Number of filled values in 'Occupation'", filled_occupation)
print("Number of filled values in 'Satisfaction_Level'", filled_satisfaction)
df['Occupation'].fillna(df['Occupation'].mode()[0], inplace = True)
df
df['Satisfaction_Level'].fillna(df['Satisfaction_Level'].median(), inplace = True)
df
df['Satisfaction_Binary'] = df['Satisfaction_Level'].apply(lambda x: 'High' if x > 0.7
else 'Low')
df
purchase_map = {'Low' : 0, 'Medium' : 1, 'High' : 2}
df['Purchase_Numeric'] = df['Purchase_History'].map(purchase_map)
df
Q1 = df['Income'].quantile(0.25)
Q3 = df['Income'].quantile(0.75)
IQR = Q3 - Q1
lower_bound = Q1 - 1.5 * IQR
upper_bound = Q3 + 1.5 * IQR
outliers = df[(df['Income'] < lower_bound) | (df['Income'] > upper_bound)]
print("Outliers in 'Income' column:")
outliers
print("Number of outliers: ", outliers['ID'].count())
df['Years_Employed'].fillna(df['Years_Employed'].median(), inplace = True)
df
Program 2
import pandas as pd
import numpy as np
df = pd.read_csv('dav_lab2.csv')
[Link]()
def missing_values(df):
df['Age'].fillna(df['Age'].mean(), inplace = True)
df['City'].fillna('Unknown', inplace = True)
return df
df = missing_values(df)
print([Link]().sum())
df
print(f"Number of rows before removing duplicate rows: {len(df)}")
display(df)
def remove_duplicates(df):
df = [Link]()
df = df.drop_duplicates()
return df
df = remove_duplicates(df)
print(f"\nNumber of rows after removing duplicates: {len(df)}")
df
def standardize_gender(df):
gender_map = {'M': 'Male', 'F': 'Female'}
df['Gender'] = df['Gender'].replace(gender_map)
return df
df = standardize_gender(df)
df
print("Original Dataframe")
display(df)
def group_age(df):
df = [Link]()
if 'Age' in [Link]:
bins = [0, 18, 30, 40, 50, [Link]]
labels = ['0-17', '18-29', '30-39', '40-49', '50+']
df['Age_Group'] = [Link](df['Age'], bins = bins, labels = labels, right =
False)
return df
df = group_age(df)
print("\nDataframe after grouping age")
df
print("Original Dataframe")
display(df)
def city_to_dummies(df):
df = [Link]()
if 'City' in [Link]:
dummies = pd.get_dummies(df['City'], prefix = 'City')
df = [Link]([df, dummies], axis = 1)
return df
df = city_to_dummies(df)
print("\nDataframe after converting city to dummy variable:")
df
Program 3
import pandas as pd
import numpy as np
sales_df = pd.read_csv("[Link]")
feedback_df = pd.read_csv("[Link]")
print("Sales Data Shape:", sales_df.shape)
print("Customer Feedback Shape:", feedback_df.shape)
print("\nSales Data Columns:", sales_df.[Link]())
print("\nCustomer Feedback Columns:", feedback_df.[Link]())
sales_df
feedback_df
#1
def create_hierarchical_index(df):
if 'Product' in [Link] and 'Month' in [Link]:
print("\nHierarchical Index Created Successfully")
df_hierarchical = df.set_index(['Product', 'Month'])
return df_hierarchical
else:
print("Required columns 'Product' and/or 'Month' not found")
return df
sales_hierarchical = create_hierarchical_index(sales_df)
print(f"\n{sales_hierarchical}")
if isinstance(sales_hierarchical.index, [Link]):
print("\nHierarchical Index Example - Subsetting:")
try:
user_product = input("Enter a product name to view its sales: ")
product_sales = sales_hierarchical.xs(user_product, level='Product')
print(f"Sales for {user_product}:\n{product_sales}")
except KeyError:
print(f"Product '{user_product}' not found in the data. Please check
available products")
except Exception as e:
print(f"Error: {e}")
#2
def demonstrate_merges(sales_df, feedback_df):
# Keep only necessary fields from feedback for clarity
fb = feedback_df[["OrderID", "CustomerID", "Feedback_Score"]].copy()
# Inner Join
inner_merged = [Link](sales_df, fb, on="OrderID", how="inner")
print(f"\nInner Join: {inner_merged.shape} rows")
print(f"\n{inner_merged}\n")
# Left Join
left_merged = [Link](sales_df, fb, on="OrderID", how="left")
print(f"Left Join: {left_merged.shape} rows")
print(f"\n{left_merged}\n")
# Right Join
right_merged = [Link](sales_df, fb, on="OrderID", how="right")
print(f"Right Join: {right_merged.shape} rows")
print(f"\n{right_merged}\n")
# Outer Join
outer_merged = [Link](sales_df, fb, on="OrderID", how="outer")
print(f"Outer Join: {outer_merged.shape} rows")
print(f"\n{outer_merged}\n")
demonstrate_merges(sales_df, feedback_df)
def demonstrate_concatenation():
print("\nConcatenation Operations:\n")
# Create three example "quarters" by slicing the sample data
quarter1 = sales_df.iloc[:1].copy()
quarter2 = sales_df.iloc[1:3].copy()
quarter3 = sales_df.iloc[3:].copy()
print(f"\nQuarter-1\n{quarter1}\n")
print(f"\nQuarter-2\n{quarter2}\n")
print(f"\nQuarter-3\n{quarter3}\n")
quarter1["Quarter"] = "Q1"
quarter2["Quarter"] = "Q2"
quarter3["Quarter"] = "Q3"
vertical_concatenated = [Link]([quarter1, quarter2, quarter3], axis=0)
print(f"Vertical Concatenation: {vertical_concatenated.shape[0]} rows")
print("Implication: Combining data from different time periods (quarterly,
monthly, or daily reports)")
print(vertical_concatenated)
sales_metrics = sales_df[['OrderID', 'Sales']].set_index('OrderID')
product_metrics = sales_df[['OrderID', 'Product']].set_index('OrderID')
horizontal_concatenated = [Link]([sales_metrics, product_metrics],
axis=1)
print(f"\nHorizontal Concatenation: {horizontal_concatenated.shape[1]}
columns")
print("Implication: Adds new attributes to existing records")
print("Note: Combining different metrics for the same entities")
print(horizontal_concatenated)
return vertical_concatenated, horizontal_concatenated
vertical_result, horizontal_result = demonstrate_concatenation()
#4
def combine_with_feedback_fill(sales_df, feedback_df):
print("\nCombining Data with Missing Value Filling:")
merged = [Link](sales_df, feedback_df, on="OrderID", how="left")
if "Feedback_Score" in [Link]:
merged["Feedback_Score"] = merged["Feedback_Score"].fillna(0)
print("Missing Feedback_Score values filled with 0")
else:
print("Feedback_Score column not found in merged data")
return merged
combined_df = combine_with_feedback_fill(sales_df, feedback_df)
print("\n", combined_df)
#5
def create_sales_pivot(df):
print("\nPivot Table Creation:")
# Validate required columns
required_cols = {"Product", "Month", "Sales"}
if not required_cols.issubset([Link]):
print("Error: Required columns missing for pivot table")
return None
try:
pivot_table = pd.pivot_table(df, index="Product", columns="Month",
values="Sales", aggfunc="sum", fill_value=0)
print("Pivot Table created successfully")
return pivot_table
except Exception as e:
print(f"Error creating pivot table: {e}")
return None
pivot_table = create_sales_pivot(sales_df)
if pivot_table is not None:
print(f"\nPivot Table Shape: {pivot_table.shape}")
print("\nPivot Table:")
print(pivot_table)
print("\nSALES SUMMARY")
# Total sales per product (row sums)
product_totals = pivot_table.sum(axis=1)
print("\nTotal Sales by Product:")
for product, total in product_totals.items():
print(f"{product}: Rs.{total:.2f}")
# Total sales per month (column sums)
month_totals = pivot_table.sum(axis=0)
print("\nTotal Sales by Month:")
for month, total in month_totals.items():
print(f"{month}: Rs.{total:.2f}")
# Grand total
grand_total = float(pivot_table.[Link]())
print("\nGrand Total Sales: Rs.{:.2f}".format(grand_total))
print("\nANALYSIS COMPLETE")
Program 5
import pandas as pd
import numpy as np
df = pd.read_csv("simple_sales_data.csv")
print("Total Revenue by Salesperson on Each Date:")
revenue_by_salesperson_date = df.pivot_table(values = 'revenue', index = 'date',
columns = 'salesperson', aggfunc = 'sum', fill_value = 0)
print(revenue_by_salesperson_date)
print("Average Revenue per Sale for Each Product")
avg_revenue_per_product = [Link]('product')['revenue'].mean().round(2)
print(avg_revenue_per_product)
print("Maximum Units Sold in Single Transaction by Each Salesperson:")
max_units_by_salesperson = [Link]('salesperson')['units_sold'].max()
print(max_units_by_salesperson)
print("Total Revenue by Each Region:")
total_revenue = df['revenue'].sum()
print(total_revenue)
revenue_by_region = [Link]('region')['revenue'].sum()
print(f"\n{revenue_by_region}\n")
print("Percentage of Total Revenue contributed by Each Region:")
percentage_by_region = (revenue_by_region / total_revenue * 100).round(2)
print(percentage_by_region)
print("Salesperson with Most Transactions:")
transaction_count = [Link]('salesperson')['transaction_id'].count()
most_transactions = transaction_count.idxmax()
max_transactions = transaction_count.max()
print(f"{most_transactions} completed the most transactions:
{max_transactions}")
print("\nFull Transaction Count:")
print(transaction_count)
print("Total Revenue and Units Sold by Salesperson for Each Product:")
salesperson_product_summary = df.pivot_table(index = 'salesperson', columns =
'product', values = ['revenue', 'units_sold'], aggfunc = 'sum', fill_value = 0)
print(salesperson_product_summary)
print("Total Units Sold in Each Region on Each Date:")
units_by_region_date = df.pivot_table(values = 'units_sold', index = 'date',
columns = 'region', aggfunc = 'sum', fill_value = 0)
print(units_by_region_date)
Program 6
import pandas as pd
import [Link] as plt
import seaborn as sns
import numpy as np
games_df = pd.read_csv('basketball_games.csv')
players_df = pd.read_csv('basketball_players.csv')
games_df['Date'] = pd.to_datetime(games_df['Date'])
print("1. Team's Points Score Over the Season:")
[Link](figsize=(12, 6))
[Link](games_df['Date'], games_df['Team_Points'],
marker='x', linewidth=2, markersize=8)
[Link]("Team's Points Score Over the Season", fontsize=10, fontweight='bold')
[Link]('Date', fontsize=10)
[Link]('Points Scored', fontsize=12)
[Link](True)
[Link](rotation=45)
plt.tight_layout()
[Link]()
trend=[Link](range(len(games_df)),games_df['Team_Points'],1)[0]
trend_direction="improving" if trend>0 else "declining" if trend <0 else "stable"
print(f"Points trend : {trend_direction} (slope: {trend:.2f})")
print(f"\n2. Average Attendance: {games_df['Attendance'].mean():.0f} people")
print(f" Highest Attendance: {games_df['Attendance'].max():.0f} people")
print(f" Lowest Attendance: {games_df['Attendance'].min():.0f} people")
[Link](figsize=(10, 6))
[Link](games_df['Attendance'], bins=8, alpha=0.7, edgecolor='black')
[Link]('Distribution of Game Attendance', fontsize=16, fontweight='bold')
[Link]('Attendance', fontsize=12)
[Link]('Number of Games', fontsize=12)
[Link](True)
plt.tight_layout()
[Link]()
player_points = players_df.groupby('Player')
['Points'].sum().sort_values(ascending=False)
top_scorer = player_points.index[0]
top_score = player_points.iloc[0]
print(f"\n3. Top Scorer: {top_scorer} with {top_score} total points")
[Link](figsize=(10, 6))
player_points.plot(kind='bar', color=['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728',
'#9467bd'])
[Link]('Total Points Scored by Each Player', fontsize=16, fontweight='bold')
[Link]('Player', fontsize=12)
[Link]('Total Points', fontsize=12)
[Link](True)
plt.tight_layout()
[Link]()
threshold = 100
games_above_threshold = len(games_df[games_df['Team_Points'] > threshold])
print(f"\nGames scoring above {threshold} points:
{games_above_threshold}/{len(games_df)}")
bins = [80, 90, 100, 110, 120]
labels = ['80-89', '90-99', '100-109', '110-119']
games_df['Point_Range'] = [Link](games_df['Team_Points'], bins=bins,
labels=labels, right=False)
point_range_counts = games_df['Point_Range'].value_counts().sort_index()
[Link](figsize=(10, 6))
point_range_counts.plot(kind='bar', color='skyblue', edgecolor='black')
[Link]('Number of Games by Points Scored Range', fontsize=16,
fontweight='bold')
[Link]('Points Range', fontsize=12)
[Link]('Number of Games', fontsize=12)
[Link](True,axis="y")
plt.tight_layout()
[Link]()
opponent_performance = games_df.groupby('Opponent').agg({
'Team_Points': 'mean',
'Result': lambda x: (x == 'Win').sum()
}).round(1)
opponent_performance.columns = ['Avg_Points_Against', 'Wins']
opponent_performance =
opponent_performance.sort_values('Avg_Points_Against', ascending=False)
print(f"\nTeam Performance Against Opponents (by average points scored):")
display(opponent_performance.reset_index())
[Link](figsize=(12, 6))
opponent_performance['Avg_Points_Against'].sort_values().plot(kind='barh',
color='Blue')
[Link]('Average Points Scored Against Each Opponent', fontsize=16,
fontweight='bold')
[Link]('Average Points Scored', fontsize=12)
[Link]('Opponent', fontsize=12)
[Link](True, axis='x')
plt.tight_layout()
[Link]()
opponent_attendance = games_df.groupby('Opponent')
['Attendance'].mean().sort_values(ascending=False)
print(f"\n Average Attendance by Opponent:")
for opponent, attendance in opponent_attendance.items():
print(f" {opponent}: {attendance:.0f} average attendance")
[Link](figsize=(12, 6))
opponent_attendance.sort_values().plot(kind='barh', color='orange')
[Link]('Average Attendance by Opponent', fontsize=16, fontweight='bold')
[Link]('Average Attendance', fontsize=12)
[Link]('Opponent', fontsize=12)
[Link](True, alpha=0.3, axis='x')
plt.tight_layout()
[Link]()
win_loss_stats = games_df.groupby('Result').agg({
'Team_Points': ['count', 'mean'],
'Game_ID': 'count'
}).round(1)
win_loss_stats.columns = ['Games_Count', 'Avg_Points', 'Total_Games']
win_loss_stats = win_loss_stats.reset_index()
print(f"\n Win-Loss Record vs Points Scored:")
for _,row in win_loss_stats.iterrows():
print(f" {row['Result']}: {row['Games_Count']} games, {row['Avg_Points']}
avg points")
fig, (ax1, ax2) = [Link](1, 2, figsize=(14, 6))
results = win_loss_stats['Result']
game_counts = win_loss_stats['Games_Count']
colors = ['green' if result == 'Win' else 'red' if result == 'Loss' else 'gray' for result in
results]
[Link](results, game_counts, color=colors, alpha=0.7, edgecolor='black')
ax1.set_title('Win-Loss-Tie Record', fontsize=14, fontweight='bold')
ax1.set_ylabel('Number of Games', fontsize=12)
[Link](True, alpha=0.3, axis='y')
avg_points = win_loss_stats['Avg_Points']
[Link](results, avg_points, color=colors, alpha=0.7, edgecolor='black')
ax2.set_title('Average Points by Game Result', fontsize=14, fontweight='bold')
ax2.set_ylabel('Average Points', fontsize=12)
[Link](True, alpha=0.3, axis='y')
plt.tight_layout()
[Link]()
Program 7
import pandas as pd
import [Link] as plt
import seaborn as sns
uber_df = pd.read_csv('[Link]')
uber_df['date'] = pd.to_datetime(uber_df['date'], format='mixed', dayfirst=True)
uber_df['day_of_week'] = uber_df['date'].[Link]
uber_df['month'] = uber_df['date'].[Link]
heatmap_data = uber_df.groupby(['day_of_week', 'month'])
['trips'].sum().reset_index()
heatmap_data_pivot = heatmap_data.pivot_table(index='day_of_week',
columns='month', values='trips').fillna(0)
uber_df['hour'] = uber_df['date'].[Link]
heatmap_data_hour = uber_df.groupby(['day_of_week', 'hour'])
['trips'].sum().reset_index()
heatmap_data_pivot_hour =
heatmap_data_hour.pivot_table(index='day_of_week', columns='hour',
values='trips').fillna(0)
uber_df['day'] = uber_df['date'].[Link]
monthly_data = uber_df[uber_df['month'] == 1] # Filtering for January
line_chart_data = monthly_data.groupby('day')['trips'].sum().reset_index()
bubble_chart_data = uber_df.groupby('dispatching_base_number')
['trips'].sum().reset_index()
# Heatmap
[Link](figsize=(12, 8))
[Link](heatmap_data_pivot_hour, cmap='viridis', annot=False, fmt=".0f")
[Link]('Hour of Day')
[Link]('Day of Week (0=Monday, 6=Sunday)')
[Link]('Uber Pickups Heatmap by Day of Week and Hour')
[Link]()
# Line Chart
[Link](figsize=(10, 6))
[Link](line_chart_data['day'], line_chart_data['trips'], marker='o')
[Link]('Day of Month')
[Link]('Number of Trips')
[Link]('Uber Pickups Trend in January')
[Link](True)
[Link]()
# Bubble Chart
[Link](figsize=(12, 7))
[Link](bubble_chart_data['dispatching_base_number'],
bubble_chart_data['trips'], s=bubble_chart_data['trips']/1000, alpha=0.7)
[Link]('Dispatching Base Number')
[Link]('Total Trips')
[Link]('Uber Pickups by Dispatching Base Number')
[Link](rotation=45, ha='right')
[Link](True, linestyle='--', alpha=0.6)
plt.tight_layout()
[Link]()
Program 8
import pandas as pd
import numpy as np
import [Link] as plt
df = pd.read_csv('[Link]')
df
[Link]()
zero_cols = [col for col in [Link] if 'zero' in col]
df = [Link](columns = zero_cols)
df
survival_rate = [Link]('Pclass')['Survived'].mean()
[Link]()
survival_rate.plot(kind='bar')
[Link]('Pclass')
[Link]('Survival Rate')
[Link]('Survival Rate by Passenger Class')
plt.tight_layout()
[Link]()
counts = df['Survived'].value_counts()
[Link]()
[Link](counts, labels=['Non-Survivor', 'Survivor'], autopct='%1.1f%%')
[Link]('Proportion of Survivors vs Non-Survivors')
plt.tight_layout()
[Link]()
grouped = [Link](['Pclass', 'Sex', 'Survived']).size().unstack(fill_value=0)
[Link]()
[Link]('Sex').plot(kind='bar', stacked=True)
[Link]('Pclass')
[Link]('Count')
[Link]('Survivors and Non-Survivors by Class and Sex')
plt.tight_layout()
[Link]()
Program 9
import pandas as pd
import [Link] as plt
df = pd.read_csv("Dataset_9.csv")
[Link](figsize=(8,6))
[Link](df["GrLivArea"], df["SalePrice"])
[Link]("GrLivArea")
[Link]("SalePrice")
[Link]("GrLivArea vs SalePrice")
[Link]()
import [Link] as plt
numerical_cols = ["GrLivArea", "OverallQual", "TotalBsmtSF", "SalePrice",
"YearBuilt"]
corr = df[numerical_cols].corr()
[Link](figsize=(7,6))
[Link](corr, aspect='auto')
[Link]()
[Link](range(len(numerical_cols)), numerical_cols, rotation=45)
[Link](range(len(numerical_cols)), numerical_cols)
[Link]("Correlation Heatmap")
[Link]()
[Link](figsize=(8,6))
[Link](df["GrLivArea"], df["SalePrice"], s=df["OverallQual"] * 20)
[Link]("GrLivArea")
[Link]("SalePrice")
[Link]("GrLivArea vs SalePrice with Bubble Size = OverallQual")
[Link]()
Program 10
import pandas as pd
import [Link] as plt
df = pd.read_csv("Dataset_10.csv")
# Identify all position rating columns
position_cols = [
'gk', 'st', 'lw', 'cf', 'rw', 'cam', 'lm', 'lcm', 'cm', 'rcm', 'rm',
'lwb', 'ldm', 'cdm', 'rdm', 'rwb', 'lb', 'lcb', 'cb', 'rcb', 'rb'
]
# Filter position_cols to only include columns that exist in the DataFrame
existing_position_cols = list([Link](position_cols))
df["BestPosition"] = df[existing_position_cols].idxmax(axis=1)
import [Link] as plt
pos_counts = df["BestPosition"].value_counts()
[Link](figsize=(12, 6))
[Link](pos_counts.index, pos_counts.values)
[Link]("Position")
[Link]("Number of Players")
[Link]("Player Count by Best Position")
[Link](rotation=45)
plt.tight_layout()
[Link]()
bins = [0, 60, 70, 80, 90, 100]
labels = ["0–60", "61–70", "71–80", "81–90", "91–100"]
df["RatingRange"] = [Link](df["overall"], bins=bins, labels=labels,
include_lowest=True)
rating_dist = df["RatingRange"].value_counts().s ort_index()
[Link](figsize=(7,7))
[Link](rating_dist.values, labels=rating_dist.index, autopct='%1.1f%%')
centre = [Link]((0,0), 0.6, color="white")
[Link]().add_artist(centre)
[Link]("Overall Rating Distribution (Donut Chart)")
[Link]()
import squarify
import [Link] as plt
tree_data = [Link]("BestPosition")
["overall"].sum().sort_values(ascending=False)
[Link](figsize=(14, 8))
[Link](sizes=tree_data.values, label=tree_data.index,
value=tree_data.values)
[Link]("Treemap: Total Overall Rating by Position")
[Link]("off")
[Link]()