-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_deom.py
More file actions
175 lines (135 loc) · 5.1 KB
/
Copy pathpython_deom.py
File metadata and controls
175 lines (135 loc) · 5.1 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
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
# JSON File Loading
data_json = pd.read_json("file_name.json")
# SQL Database Loading
import sqlite3
connection_db = sqlite3.connect("database_name.db")
query1 = 'SELECT col_name FROM row_name' # to just display 1 column
query2 = 'SELECT * FROM row_name' # to display all colums
data_sql = pd.read_sql(query1, connection_db)
data_sql = pd.read_sql(query2, connection_db)
import pandas as pd
import numpy as np
# Excel File Loading
data_xl = pd.read_excel(r"C:\Users\ASUS\OneDrive\Documents\PythonForDS\IPL.xlsx", sheet_name = "IPL Matches")
print(data_xl.head())
data_xl1 = pd.read_excel(r"C:\Users\ASUS\OneDrive\Documents\PythonForDS\IPL.xlsx", sheet_name = "IPL Deliveries")
print(data_xl.head())
# TXT File Loading
data_txt = pd.read_csv("students.txt", header = 0, sep = ',')
print(data_txt)
data_txt1 = pd.read_csv("employee.txt", header = 0, sep = ',')
print(data_txt1)
data_txt2 = pd.read_csv("department.txt", header = 0, sep = ',')
print(data_txt2)
# CSV File Loading and Reading
data = pd.read_csv("percent-bachelors-degrees-women-usa.csv")
print(data)
print(data.columns)
print(data.columns.to_list())
# Head Functionality
print(data.head())
print(data.head(10))
# Tail Functionality
print(data.tail())
print(data.tail(10))
# Info Funtion
print(data.info())
# Drop all missing values
print(data.dropna())
# Fill all missing values
print(data.fillna("NULL"))
# Drop Duplicates
print(data.drop_duplicates())
# Access specific Row
print(data.iloc[29])
print(data.iloc[2:10]) # Rows from 2 to 9 (10 not included)
# Access specific Column
print(data['Biology'])
print(data.loc[:,'Biology']) # All Rows, Biology Column
print(data.loc[:,'Architecture':'Physical Sciences']) # All Rows, Architectur to Physical Sciences Column
# Accessing just the specific value in Row and Column
print(data.loc[10, 'Engineering'])
# Both Together (loc & iloc)
print(data.loc[10:20,'Business':'English']) # Rows (10 to 20), Columns (Business to English)
# Data Aggregation {Sorting, Filtering, Grouping}
# Data Sorting
print(data.sort_values(by='Art and Performance', ascending=True)) # Values arranged in Ascending
print(data_txt.sort_values(by='SchoolID', ascending=False)) # Values arranged in Descending
print(data_txt1.sort_values(by='Salary', ascending=False)) # Values arranged in Descending
print(data_txt1.sort_values(by='Age', ascending=False)) # Values arranged in Descending
# Data Grouping
print(data_txt.groupby("Country").count()) # Country wise count
print(data_txt1.groupby("Dept")["Salary"].mean()) # Dept wise Avg Salry
print(data_txt1.groupby("Dept")["Salary"].max()) # Dept wise Max Salary
# Data Filtering
print(data_txt1[data_txt1["Salary"] > 50000]) # Salary > 50k
print(data_txt1[(data_txt1["Salary"] > 50000) & (data_txt1["Salary"] < 70000)]) # Salary between 50k and 70k
print(data_txt1[data_txt1["Age"].isin([27,29])]) # Employees whise age is 27 and 29
# Descriptive Statistics
print(data.describe()) # Gives stats related info
# Mean
print("Mean of the Data:", np.mean(data))
# Median
print("Median of the Data:", np.median(data))
# Varaince
print("Variance of the Data:", np.var(data))
# Standard Deviation (SD)
print("SD of the Data:", np.std(data))
# Mode (uses scipy and not numpy)
from scipy import stats
print("Mode of the Data:", stats.mode(data))
# Data Merging
print(pd.merge(data_txt1, data_txt2, on='Dept', how='inner')) # Inner Join
print(pd.merge(data_txt1, data_txt2, on='Dept', how='left')) # Left Join
print(pd.merge(data_txt1, data_txt2, on='Dept', how='right')) # Right Join
print(pd.merge(data_txt1, data_txt2, how= 'cross')) # Cross Join
# Left Anti Join
print(pd.merge(data_txt1, data_txt2, on='Dept', how='left', indicator=True)
.query("_merge == 'left_only'")
.drop('_merge', axis=1))
# Right Anti Join
print(pd.merge(data_txt1, data_txt2, on='Dept', how='right', indicator=True)
.query("_merge == 'right_only'")
.drop('_merge', axis=1))
# Data Visualization
import matplotlib.pyplot as plt
x_values = [1,2,3,4,5,6,7,8,9,10]
y_values = [2,4,6,8,10,12,14,16,18,20]
cat = ["Cat", "Dog", "Horse", "Mouse"]
cat_values = [10, 20, 100, 5]
# Line Graph
plt.plot(x_values,y_values, color = 'green')
plt.xlabel("X axis")
plt.ylabel("Y axis")
plt.title("Line Graph Representation")
plt.show()
# Scatter Graph
plt.scatter(x_values,y_values, color = 'green')
plt.xlabel("X axis")
plt.ylabel("Y axis")
plt.title("Scatter Graph Representation")
plt.show()
# Bar Chart
plt.bar(cat, cat_values, color = 'green')
plt.xlabel("X axis - Aniaml")
plt.ylabel("Y axis - Animal Weight")
plt.title("Bar Graph Representation of Aniaml Weight")
plt.show()
# Histograms
x_normal = np.random.normal(0,1,100)
plt.hist(x_normal, color='green')
plt.xlabel("X axis")
plt.ylabel("Y axis")
plt.title("Histogram Representation")
plt.show()
# Population Distribution
from scipy.stats import norm
x_val = np.arange(-4, 4, 0.01)
y_val = norm.pdf(x_val)
counts, bins, ignored = plt.hist(x_normal, 30, density=True, color='green', label='Sampling Distribution')
plt.plot(x_val, y_val, color='y', linewidth=2.5, label='Population Distribution')
plt.title("Population Distribution")
plt.xlabel("X axis")
plt.ylabel("Y axis")
plt.legend()
plt.show()