-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcustomdata.py
59 lines (44 loc) · 1.25 KB
/
customdata.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
from pyspark.sql import SparkSession
from pyspark.sql.functions import avg, max, min
import sys
from pyspark.sql import Row
from pyspark.sql.types import *
if __name__ == '__main__':
spark = (SparkSession
.builder
.appName(sys.argv[0])
.getOrCreate())
#create sample data as an array
data = [
['Jack', 50, 'Uber'],
['Emily', 24, 'Amazon'],
['Krishan', 30, 'Microsoft']
]
#define explicit schema
schema = 'name STRING , age INT, `company name` STRING'
df = spark.createDataFrame(data,schema)
df.show()
print('Age statistics : ')
df.agg(avg('age'), max('age'), min('age')).show()
print('The schema is :')
df.printSchema()
# Rows
data = [
Row('Krishan', 30, 'Microsoft'),
Row('Ram',30,'Amazon'),
Row('Sweta',24,'Amazon')
]
data = [
Row('Krishan', 30, 'Microsoft'),
Row('Ram',30,'Amazon'),
Row('Sweta',24,'Amazon')
]
SCHEMA = StructType(
[
StructField('Name', StringType(), False),
StructField('Age',IntegerType(), True),
StructField('Company', StringType(), True)
]
)
df = spark.createDataFrame(data, schema = SCHEMA)
df.show()