-
Notifications
You must be signed in to change notification settings - Fork 474
/
Copy pathdataframe-session-2021-05-12-intro.txt
193 lines (171 loc) · 4.14 KB
/
dataframe-session-2021-05-12-intro.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
#--------------------
# DataFrame Tutorial:
#--------------------
https://dzone.com/articles/pyspark-dataframe-tutorial-introduction-to-datafra
#---------------------
# Demo of DataFrames
#---------------------
$ cat /tmp/cats.csv
name,age,gender,weight
cuttie,2,female,6
mono,3,male,9
pishi,2,female,4
zazo,1,male,4
fuzzy,1,female,4
$ ./bin/pyspark
Python 3.7.2 (v3.7.2:9a3ffc0492, Dec 24 2018, 02:44:43)
Welcome to
____ __
/ __/__ ___ _____/ /__
_\ \/ _ \/ _ `/ __/ '_/
/__ / .__/\_,_/_/ /_/\_\ version 3.1.1
/_/
Using Python version 3.7.2 (v3.7.2:9a3ffc0492, Dec 24 2018 02:44:43)
Spark context Web UI available at http://10.0.0.93:4040
Spark context available as 'sc' (master = local[*], app id = local-1620755686906).
SparkSession available as 'spark'.
>>>
>>> input_path = '/tmp/cats.csv'
>>> input_path
'/tmp/cats.csv'
>>> cats = spark.read.csv(input_path, inferSchema = True, header = True)
>>>
>>> cats.show(truncate=False)
+------+---+------+------+
|name |age|gender|weight|
+------+---+------+------+
|cuttie|2 |female|6 |
|mono |3 |male |9 |
|pishi |2 |female|4 |
|zazo |1 |male |4 |
|fuzzy |1 |female|4 |
+------+---+------+------+
>>> cats.printSchema()
root
|-- name: string (nullable = true)
|-- age: integer (nullable = true)
|-- gender: string (nullable = true)
|-- weight: integer (nullable = true)
>>> cats.count()
5
>>> cats.columns
['name', 'age', 'gender', 'weight']
>>> cats.describe('weight').show()
+-------+------------------+
|summary| weight|
+-------+------------------+
| count| 5|
| mean| 5.4|
| stddev|2.1908902300206643|
| min| 4|
| max| 9|
+-------+------------------+
>>> name_age = cats.select("name", "age")
>>> name_age.show(truncate=False)
+------+---+
|name |age|
+------+---+
|cuttie|2 |
|mono |3 |
|pishi |2 |
|zazo |1 |
|fuzzy |1 |
+------+---+
>>> name_age.printSchema()
root
|-- name: string (nullable = true)
|-- age: integer (nullable = true)
>>> cats.select('age').distinct().show()
+---+
|age|
+---+
| 1|
| 3|
| 2|
+---+
>>> cats.select('name', 'age').distinct().show()
+------+---+
| name|age|
+------+---+
| zazo| 1|
|cuttie| 2|
| fuzzy| 1|
| mono| 3|
| pishi| 2|
+------+---+
>>> cats.filter(cats.age > 1).show()
+------+---+------+------+
| name|age|gender|weight|
+------+---+------+------+
|cuttie| 2|female| 6|
| mono| 3| male| 9|
| pishi| 2|female| 4|
+------+---+------+------+
>>> cats.orderBy(cats.age).show()
+------+---+------+------+
| name|age|gender|weight|
+------+---+------+------+
| zazo| 1| male| 4|
| fuzzy| 1|female| 4|
|cuttie| 2|female| 6|
| pishi| 2|female| 4|
| mono| 3| male| 9|
+------+---+------+------+
>>> age_df = cats.groupby("age").count()
>>> age_df.show()
+---+-----+
|age|count|
+---+-----+
| 1| 2|
| 3| 1|
| 2| 2|
+---+-----+
>>> cats.show()
+------+---+------+------+
| name|age|gender|weight|
+------+---+------+------+
|cuttie| 2|female| 6|
| mono| 3| male| 9|
| pishi| 2|female| 4|
| zazo| 1| male| 4|
| fuzzy| 1|female| 4|
+------+---+------+------+
>>> cats.registerTempTable('cats_table')
>>> spark.sql("select * from cats_table").show()
+------+---+------+------+
| name|age|gender|weight|
+------+---+------+------+
|cuttie| 2|female| 6|
| mono| 3| male| 9|
| pishi| 2|female| 4|
| zazo| 1| male| 4|
| fuzzy| 1|female| 4|
+------+---+------+------+
>>> spark.sql("select * from cats_table where age > 1").show()
+------+---+------+------+
| name|age|gender|weight|
+------+---+------+------+
|cuttie| 2|female| 6|
| mono| 3| male| 9|
| pishi| 2|female| 4|
+------+---+------+------+
>>> spark.sql("select age, count(*) from cats_table group by age").show()
+---+--------+
|age|count(1)|
+---+--------+
| 1| 2|
| 3| 1|
| 2| 2|
+---+--------+
>>> def exec_sql(query):
... spark.sql(query).show()
...
>>>
>>> exec_sql("select age, count(*) from cats_table group by age")
+---+--------+
|age|count(1)|
+---+--------+
| 1| 2|
| 3| 1|
| 2| 2|
+---+--------+