-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_analysis
More file actions
101 lines (72 loc) · 2.3 KB
/
Copy pathdata_analysis
File metadata and controls
101 lines (72 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import pandas as pd
import matplotlib.pyplot as plt
# ----------------------------------
# Load Dataset
# ----------------------------------
df = pd.read_csv("/workspaces/data_analysis_project/dataset/titanic.csv")
print("First 5 Rows")
print(df.head())
print("\nDataset Shape:")
print(df.shape)
print("\nColumn Information:")
print(df.info())
# ----------------------------------
# Data Cleaning
# ----------------------------------
# Remove duplicate rows
df.drop_duplicates(inplace=True)
# Handle missing values
df["Age"] = df["Age"].fillna(df["Age"].median())
# Drop Cabin column (many missing values)
if "Cabin" in df.columns:
df.drop("Cabin", axis=1, inplace=True)
print("\nMissing Values After Cleaning")
print(df.isnull().sum())
# ----------------------------------
# Filtering
# ----------------------------------
survivors = df[df["Survived"] == 1]
print("\nNumber of Survivors:")
print(len(survivors))
# ----------------------------------
# Grouping
# ----------------------------------
survival_by_gender = df.groupby("Sex")["Survived"].mean()
print("\nSurvival Rate by Gender")
print(survival_by_gender)
survival_by_class = df.groupby("Pclass")["Survived"].mean()
print("\nSurvival Rate by Passenger Class")
print(survival_by_class)
# ----------------------------------
# Summary Statistics
# ----------------------------------
print("\nSummary Statistics")
print(df.describe())
# ----------------------------------
# Insights
# ----------------------------------
print("\nKey Insights")
highest_gender = survival_by_gender.idxmax()
highest_class = survival_by_class.idxmax()
print(f"Highest survival rate by gender: {highest_gender}")
print(f"Highest survival rate by class: Class {highest_class}")
# ----------------------------------
# Graph 1: Survival by Gender
# ----------------------------------
plt.figure(figsize=(6,4))
survival_by_gender.plot(kind="bar")
plt.title("Survival Rate by Gender")
plt.ylabel("Survival Rate")
plt.tight_layout()
plt.savefig("survival_by_gender.png")
plt.show()
# ----------------------------------
# Graph 2: Survival by Passenger Class
# ----------------------------------
plt.figure(figsize=(6,4))
survival_by_class.plot(kind="bar")
plt.title("Survival Rate by Passenger Class")
plt.ylabel("Survival Rate")
plt.tight_layout()
plt.savefig("survival_by_class.png")
plt.show()