forked from sethiv/db-files-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo-nb.py
103 lines (60 loc) · 1.74 KB
/
demo-nb.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
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
# Databricks notebook source
# MAGIC %md
# MAGIC This notebook should be used with Files on Repos onboarding documentation
# COMMAND ----------
# MAGIC %md
# MAGIC #### Working with Python modules
# COMMAND ----------
from sample import hmm
# COMMAND ----------
hmm()
# COMMAND ----------
# MAGIC %md ****Managing Python paths****
# COMMAND ----------
# MAGIC %md Refer to /db-files-examples/path-example/path-demo-nb
# COMMAND ----------
# MAGIC %md
# MAGIC #### 4/ Working with data files
# COMMAND ----------
# MAGIC %md ****Reads****
# COMMAND ----------
import csv
with open('data/winequality-red.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
print(row)
# COMMAND ----------
import pandas as pd
df= pd.read_csv("data/winequality-red.csv")
display(df)
# COMMAND ----------
# MAGIC %md
# MAGIC Installation from requirements.txt
# COMMAND ----------
pip install -r requirements.txt
# COMMAND ----------
# MAGIC %md
# MAGIC File reads via Pyspark are now allowed
# COMMAND ----------
df=spark.read.csv("file:/Workspace/Repos/[email protected]/db-files-examples/data/winequality-red.csv")
display(df)
# COMMAND ----------
# MAGIC %md
# MAGIC #### Limitations
# COMMAND ----------
# MAGIC %md
# MAGIC Cannot write to the data file from the notebook
# COMMAND ----------
import csv
list=[6,0.31,0.47,3.6,0.067,18,42,0.99549,3.39,0.66,11,6]
with open('data/winequality-red.csv', 'w') as file:
writer = csv.writer(file)
writer.writerow(list)
# COMMAND ----------
import os
os.getcwd()
# COMMAND ----------
import databricks.koalas as ks
df= ks.read_csv("file:/Workspace/Repos/[email protected]/db-files-examples/data/winequality-red.csv")
display(df)
# COMMAND ----------