-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjoins.py
More file actions
79 lines (56 loc) · 2.34 KB
/
Copy pathjoins.py
File metadata and controls
79 lines (56 loc) · 2.34 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
#Joining DataFrames please first create the DataFrames from the previous snippet
# Join departure delays data (foo) with airport info
foo.join(airportsna, airportsna.IATA == foo.origin
).select("City", "State", "date", "delay", "distance", "destination").show()
# In SQL
spark.sql("""
SELECT a.City, a.State, f.date, f.delay, f.distance, f.destination
FROM foo f
JOIN airports_na a
ON a.IATA = f.origin
""").show()
# Join departure delays data (foo) with airport info, using a cross join
foo.filter(foo.delay < 30).crossJoin(airportsna).select("City", "State", "date", "delay", "distance", "destination").show()
# In SQL
spark.sql("""
SELECT a.City, a.State, f.date, f.delay, f.distance, f.destination
FROM foo f
CROSS JOIN airports_na a
WHERE delay < 30
""").show()
# Join with Common DataFrame operations (select, filter, groupBy, orderBy, agg, avg, max, min, sum, count)
from pyspark.sql.functions import avg
from pyspark.sql.functions import max
from pyspark.sql.functions import avg, udf
from pyspark.sql.types import StringType
# Average delay per origin
avg_delay = fooBigger.groupBy("origin").agg(avg("delay").alias("avg_delay"))
avg_delay.show()
avg_delay.join(airportsna, airportsna.IATA == avg_delay.origin, "inner") \
.select("City", "State", "avg_delay") \
.show()
# Maximum delay per origin
max_delay = fooBigger.groupBy("origin").agg(max("delay").alias("max_delay"))
# Join the result with the airport info
ranked_delays = max_delay.join(airportsna, airportsna.IATA == max_delay.origin, "inner") \
.select("City", "State", "max_delay") \
.orderBy("max_delay", ascending=False)
ranked_delays.show()
# Join with UDFs
# Average delay per origin with classification
# UDF for classification
def classify_airport(delay):
if delay < 5:
return "Pünktlich"
elif delay < 20:
return "Leichte Verspätung"
else:
return "Schwere Verspätung"
# Register the UDF
classify_airport_udf = udf(classify_airport, StringType())
# Add a new column with the classification
avg_delay = avg_delay.withColumn("delay_category", classify_airport_udf(avg_delay.avg_delay))
# Join the result with the airport info
classified_airports = avg_delay.join(airportsna, airportsna.IATA == avg_delay.origin, "inner") \
.select("City", "State", "avg_delay", "delay_category")
classified_airports.show()