-
Notifications
You must be signed in to change notification settings - Fork 0
/
Basic_Data_Visualization_Project.py
186 lines (127 loc) · 4.51 KB
/
Basic_Data_Visualization_Project.py
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
# Specify the file path
file_path = 'adult.data' # Replace with the actual file path
# Read the file
with open(file_path, 'r') as file:
data = file.readlines()
# Define the column names
column_names = ['age', 'workclass', 'fnlwgt', 'education', 'education_num', 'marital_status', 'occupation',
'relationship', 'race', 'sex', 'capital_gain', 'capital_loss', 'hours_per_week',
'native_country', 'income']
# Extract data rows
rows = [row.strip().split(',') for row in data]
# Create the DataFrame
df = pd.DataFrame(rows, columns=column_names)
# Access and manipulate the data
print(df.head()) # Print the first few rows of the DataFrame
print(df.shape)
print(df.info()) #to check details about the dataset
print(df.duplicated().sum()) #to check for duplicates
df = df.drop_duplicates()#remove the 24 duplicates
print(df.duplicated().sum())
#Crosschecking for null values
print(df.isnull().sum())
df.dropna(inplace=True)#drop the null values
print(df.isnull().sum())
print(df.dtypes)
columns_to_convert = ['age', 'fnlwgt', 'education_num', 'capital_gain', 'capital_loss', 'hours_per_week']
# Loop through each column and convert the data type to int
for column in columns_to_convert:
df[column] = df[column].astype(int)
# Verify the updated data types
print(df.dtypes)
plt.figure(figsize=(35, 6))
plt.subplot(1, 3, 1)
plt1 = sns.countplot(data=df, x='education', hue='income')
plt.title('Distribution Of Income with education')
plt.xlabel('Education')
plt.ylabel('Frequency')
plt.xticks(rotation=90)
plt.show()
plt.figure(figsize=(15, 10))
sns.histplot(data=df, x='education', hue='sex', multiple='stack', palette='Set2')
# Set the x-axis label
plt.xlabel('Education and Gender')
plt.xticks(rotation=60)
plt.show()
plt.figure(figsize=(35, 6))
plt.subplot(1, 3, 1)
plt1 = sns.countplot(data=df, x='occupation', hue='income', palette='Set1')
plt.title('Distribution Of Income In Terms of Occupation')
plt.xlabel('Occupation')
plt.ylabel('Frequency')
plt.xticks(rotation=60)
plt.show()
plt.figure(figsize=(30, 10))
sns.histplot(data=df, x='occupation', hue='sex', multiple='stack', palette='Set3')
plt.xticks(rotation=60)
# Set the x-axis label
plt.xlabel('Occupation And Gender')
# Show the plot
plt.show()
# Count the frequency of each gender
gender_counts = df['sex'].value_counts()
# Show the plot
plt.show()
marital_counts = df['marital_status'].value_counts()
fig, ax = plt.subplots(figsize=(15, 10))
# Create a pie chart
plt.pie(marital_counts, labels=marital_counts.index, autopct='%1.1f%%')
# Set the title
plt.title('Marital Status Distribution')
# Show the plot
plt.show()
plt.figure(figsize=(17, 6))
# Create a box plot
sns.boxplot(x='occupation', y='hours_per_week', data=df)
# Set the title and labels
plt.title('Work Hours Distribution by Occupation')
plt.xlabel('Occupation')
plt.ylabel('Hours per Week')
# Rotate x-axis labels for better readability
plt.xticks(rotation=60)
# Show the plot
plt.show()
# Create the line graph
plt.figure(figsize=(12, 6))
sns.lineplot(data=df, x='occupation', y='hours_per_week', hue='race', linewidth=2, antialiased=False)
# Set labels and title
plt.xlabel('Occupation')
plt.ylabel('Work Hours per Week')
plt.title('Work Hours by Occupation with Race')
# Rotate x-axis labels for better visibility
plt.xticks(rotation=45)
# Show the plot
plt.show()
plt.figure(figsize=(20, 6))
# Create a histogram
plt.hist(df['hours_per_week'], bins=10)
# Set the title and labels
plt.title('Work Hours Distribution')
plt.xlabel('Hours per Week')
plt.ylabel('Frequency')
# Show the plot
plt.show()
grouped_data = df.groupby(['marital_status', 'income']).size().unstack()
plt.figure(figsize=(20, 6))
# Create a bar plot
grouped_data.plot(kind='bar', stacked=True)
# Set the title and labels
plt.title('Income Distribution by Marital Status')
plt.xlabel('Marital Status')
plt.ylabel('Count')
# Show the plot
plt.show()
grouped_data = df.groupby(['relationship', 'income']).size().unstack()
# Plot pie charts for each relationship category
for relationship in grouped_data.index:
plt.figure()
plt.pie(grouped_data.loc[relationship], labels=grouped_data.columns, autopct='%1.1f%%')
plt.title(f'Income Distribution for {relationship}')
plt.axis('equal')
# Show the plots
plt.show()