-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlead,lag assignment.txt
275 lines (223 loc) · 11.9 KB
/
lead,lag assignment.txt
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import os
from pyspark import SparkConf
from pyspark.sql.functions import col, when
from pyspark.sql import SparkSession
from pyspark import SparkContext
from pyspark.sql.types import StructType, StructField, StringType, IntegerType
from pyspark.sql.window import Window
from pyspark.sql.functions import col, initcap,when, to_date, datediff, lit,sum,avg,count, min,max,lead,lag,rank,desc
os.environ [ "PYSPARK_PYTHON"] = "C:/Users/002PV2744/Documents/Python/Python37/python.exe"
confg = SparkConf()
confg.set("spark.app.name", "spark prm")
confg.set("spark.master", "local[*]")
spark = SparkSession.builder.config(conf= confg).getOrCreate()
# Calculating total revenue in its category
# ========================================
# salesData= [
# ("Product1", "Category1", 100),
# ("Product2", "Category1", 200),
# ("Product3", "Category1", 150),
# ("Product4", "Category2", 300),
# ("Product5", "Category2", 250),
# ("Product6", "Category2", 180)
# ]
# salesData_df = spark.createDataFrame(salesData,["Product", "Category", "Revenue"])
# window =Window.partitionBy(col("Category")).orderBy(col("Revenue"))
# df = salesData_df.withColumn("max_revenue",max(col("Revenue")).over(window),col("Product"),col("Category"),col("Revenue")).show()
# df1= df.filter(max(col("Revenue")) == col("Revenue"))
# df2= df.select(col("Product"),col("Category"),col("Revenue")).show()
#
# window1 =Window.orderBy(col("Revenue").desc)
# df = salesData_df.withColumn(col("Product"),col("Category"),col("Revenue"),lead(col("revenue"),1).over(window1)).show()
# we want to find the difference between the price on each day with it’s previous day.
# =======================================================================================
salesdata = [(1, "KitKat",1000.0,"2021-01-01"),
(1, "KitKat",2000.0,"2021-01-02"),
(1, "KitKat",1000.0,"2021-01-03"),
(1, "KitKat",2000.0,"2021-01-04"),
(1, "KitKat",3000.0,"2021-01-05"),
(1, "KitKat",1000.0,"2021-01-06")]
salesdata_df = spark.createDataFrame(salesdata,["id", "Product", "price", "date"])
window = Window.orderBy(col("date"))
df = salesdata_df.withColumn("diffprice",col("price")-(lag(col("price"),1).over(window))).show()
# 2. If salary is less than previous month we will mark it as "DOWN", if salary has increased then "UP"
# ==========================================================================================================
data = [(1,"John",1000,"01/01/2016"),
(1,"John",2000,"02/01/2016"),
(1,"John",1000,"03/01/2016"),
(1,"John",2000,"04/01/2016"),
(1,"John",3000,"05/01/2016"),
(1,"John",1000,"06/01/2016")]
data_df = spark.createDataFrame(data,["id", "name", "salary", "date"])
window = Window.orderBy("date")
df1 = data_df.withColumn("diff",when(col("salary") < lag(col("salary"),1).over(window),"DOWN")
.when(col("salary") > lag(col("salary"),1).over(window), "UP")).show()
# spark.sql(
# """
# select
# IT_ID,
# IT_Name,
# Price,
# PriceDate,
# Price-lead(Price,1) over(order by Price) as status
# from
# sales
# """
# ).show()
# Calculate the lead time for each order within the same customer
# =================================================================
load = [(101,"CustomerA","2023-09-01"),
(103,"CustomerA","2023-09-03"),
(102,"CustomerB","2023-09-02"),
(104,"CustomerB","2023-09-04")]
load_df = spark.createDataFrame(load,["order_id", "customer", "order_date"])
window = Window.partitionBy("customer").orderBy("order_date")
df2 = load_df.withColumn("lead_date",lead(to_date("order_date"),1).over(window)).show()
#3. Calculate the lead and lag of the salary column ordered by id
# =================================================================
data = [(1,"karthik",1000),
(2,"moahn", 2000),
(3,"vinay", 1500 ),
(4,"Deva", 3000 )]
data_df = spark.createDataFrame(data, ["id","name","salary"])
window = Window.orderBy("id")
df = data_df.withColumn("lag_salary",lag(col("salary")).over(window)) \
.withColumn("lead_salary",lead(col("salary")).over(window)).show()
#4. Calculate the percentage change in salary from the previous row to the current row, ordered by id.
# =========================================================================================================
data = [(1,"karthik",1000),
(2,"moahn", 2000),
(3,"vinay", 1500 ),
(4,"Deva", 3000 )]
data_df = spark.createDataFrame(data, ["id","name","salary"])
window = Window.orderBy("id")
df = data_df.withColumn("lag_salary",lag(col("salary")).over(window)) \
.withColumn("percentage",((col("salary") - col("lag_salary"))/col("lag_salary")) * 100).show()
# 5. Calculate the rolling sum of salary for the current row and the previous two rows, ordered by id.
# =======================================================================================================
data = [(1,"karthik",1000),
(2,"moahn", 2000),
(3,"vinay", 1500 ),
(4,"Deva", 3000 )]
data_df = spark.createDataFrame(data, ["id","name","salary"])
window = Window.orderBy("id").rowsBetween(-2, 0)
df = data_df.withColumn("sum_salary",sum(col("salary")).over(window)).show()
# 6. Calculate the difference between the current salary and the minimum salary within the last three rows, ordered by id
# =========================================================================================================================
data = [(1,"karthik",1000),
(2,"moahn", 2000),
(3,"vinay", 1500 ),
(4,"Deva", 3000 )]
data_df = spark.createDataFrame(data, ["id","name","salary"])
window = Window.orderBy("id").rowsBetween(-2,0)
df = data_df.withColumn("min_salary",min(col("salary")).over(window)) \
.withColumn("diff_salary",col("salary") - col("min_salary")).show()
# 7. Calculate the lead and lag of salary within each group of employees (grouped by name) ordered by id.
# =========================================================================================================================
data = [(1,"karthik",1000),
(2,"moahn", 2000),
(3,"vinay", 1500 ),
(4,"Deva", 3000 )]
data_df = spark.createDataFrame(data, ["id","name","salary"])
window = Window.partitionBy("name").orderBy("id")
df = data_df.withColumn("lag_salary",lag(col("salary"),1).over(window)).withColumn("lead_salary",lead(col("salary"),1).over(window)).show()
# 8. Calculate the lead and lag of the salary column for each employee ordered by id, but only for the emplo
# yees who have a salary greater than 1500.
# # =========================================================================================================================
data = [(1,"karthik",1000),
(2,"moahn", 2000),
(3,"vinay", 1500 ),
(4,"Deva", 3000 )]
data_df = spark.createDataFrame(data, ["id","name","salary"])
df1 = data_df.filter(col("salary")>1500)
window = Window.orderBy("id")
df = df1.withColumn("lag_salary", lag(col("salary")).over(window)).withColumn("lead_salary",lead(col("salary")).over(window)).show()
# 9. Calculate the lead and lag of the salary column for each employee, ordered by id, but only for the emplo
# yees who have a change in salary greater than 500 from the previous row.
# =========================================================================================================================
data = [(1,"karthik",1000),
(2,"moahn", 2000),
(3,"vinay", 1500 ),
(4,"Deva", 3000 )]
data_df = spark.createDataFrame(data, ["id","name","salary"])
df1 = data_df.filter(col("salary")>1500)
window = Window.orderBy("id")
df = df1.withColumn("lag_salary", lag(col("salary")).over(window)).withColumn("lead_salary",lead(col("salary")).over(window)).show()
# 10. Calculate the cumulative count of employees, ordered by id, and reset the count when the name changes.
#===========================================================================================================
data = [(1,"karthik",1000),
(2,"moahn", 2000),
(3,"vinay", 1500 ),
(4,"Deva", 3000 )]
data_df = spark.createDataFrame(data, ["id","name","salary"])
window = Window.orderBy("id")
df = data_df.withColumn("count", count(col("name")).over(window)).show()
# 11. Calculate the running total of salary for each employee ordered by id.
#===========================================================================================================
data = [(1,"karthik",1000),
(2,"moahn", 2000),
(3,"vinay", 1500 ),
(4,"Deva", 3000 )]
data_df = spark.createDataFrame(data, ["id","name","salary"])
window = Window.partitionBy("name").orderBy("id")
df = data_df.withColumn("sum", sum(col("salary")).over(window)).show()
# 12. Find the maximum salary for each employee’s group (partitioned by name) and display it for each row.
#===========================================================================================================
data = [(1,"karthik",1000),
(2,"moahn", 2000),
(3,"vinay", 1500 ),
(4,"Deva", 3000 )]
data_df = spark.createDataFrame(data, ["id","name","salary"])
window = Window.partitionBy("name")
df = data_df.withColumn("max", max(col("salary")).over(window)).show()
# 13. Calculate the difference between the current salary and the average salary for each employee’s group
# (partitioned by name) ordered by id.
#===========================================================================================================
data = [(1,"karthik",1000),
(2,"moahn", 2000),
(3,"vinay", 1500 ),
(4,"Deva", 3000 )]
data_df = spark.createDataFrame(data, ["id","name","salary"])
window = Window.partitionBy("name").orderBy("id")
df = data_df.withColumn("max", col("salary") - avg(col("salary")).over(window)).show()
# 14. Calculate the rank of each employee based on their salary, ordered by salary in descending order.
#===========================================================================================================
data = [(1,"karthik",1000),
(2,"moahn", 2000),
(3,"vinay", 1500 ),
(4,"Deva", 3000 )]
data_df = spark.createDataFrame(data, ["id","name","salary"])
window = Window.orderBy(desc("salary"))
df = data_df.withColumn("rank", rank().over(window)).show()
# 15. Calculate the lead and lag of the salary column, ordered by id, but only for the employees whose salari
# es are strictly increasing (i.e., each employee’s salary is greater than the previous employee’s salary).
#===========================================================================================================
data = [(1,"karthik",1000),
(2,"moahn", 2000),
(3,"vinay", 1500 ),
(4,"Deva", 3000 )]
data_df = spark.createDataFrame(data, ["id","name","salary"])
window = Window.orderBy("id")
df = data_df.withColumn("lag_sal", lag(col("salary")).over(window)).withColumn("lead_sal", lead(col("salary")).over(window))
df1 = df.withColumn("increase", col("salary") > col("lag_sal")).show()
# 16. Calculate the lead and lag of the salary column ordered by id, but reset the lead and lag values when t
# he employee’s name changes.
#===========================================================================================================
data = [(1,"karthik",1000),
(2,"moahn", 2000),
(3,"vinay", 1500 ),
(4,"Deva", 3000 )]
data_df = spark.createDataFrame(data, ["id","name","salary"])
window = Window.partitionBy("name").orderBy("id")
df = data_df.withColumn("lag_sal", lag(col("salary")).over(window)).withColumn("lead_sal", lead(col("salary")).over(window)).show()
# 17. Calculate the percentage change in salary from the previous row to the current row, ordered by id, but
# group the percentage changes by name.
#===========================================================================================================
data = [(1,"karthik",1000),
(2,"moahn", 2000),
(3,"vinay", 1500 ),
(4,"Deva", 3000 )]
data_df = spark.createDataFrame(data, ["id","name","salary"])
window = Window.partitionBy("name").orderBy("id")
df = data_df.withColumn("lag_sal", lag(col("salary")).over(window))
df1 = df.withColumn("diff", ((col("salary") - col("lag_sal"))/col("lag_sal"))*100).show()