This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
l = [[0, 1, 3, 4, 5], | |
[0, 1, 2, 3], | |
[0, 3, 4, 5, 6]] | |
print([e for l in ll for e in l]) # [0, 1, 3, 4, 5, 0, 1, 2, 3, 0, 3, 4, 5, 6] | |
# Key terms: list comprehension |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
# Set a seed number for replicable results | |
random.seed(3210) | |
random.uniform(1.5, 1.9) # 1.795802587856317 | |
random.uniform(1.5, 1.9) # 1.795802587856317 | |
random.randint(1, 2) # 1 or 2 | |
np.random.randint(1, 3, 100) # array, len(100), range(0, 3) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
l = [1,2,3,2,2,2,3,4] | |
print(list(filter(lambda x: x != 2, l))) # [1, 3, 3, 4] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Here, updating l2 will also update l | |
l = [1,2,3] | |
l2 = l | |
l2[0] += 1 | |
print(l) # [2,2,3] | |
print(l2) # [2,2,3] | |
# Here, updating l2 will not update l | |
l = [1,2,3] | |
l2 = l.copy() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pprint | |
d = {'math': 99, 'english': 80, 'chemistry': 67, 'biology': 88, 'physics': 93} | |
print(d) # {'math': 99, 'english': 80, 'chemistry': 67, 'biology': 88, 'physics': 93} | |
pprint.pprint(d, width=1) | |
"""{'biology': 88, | |
'chemistry': 67, | |
'english': 80, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
# Set seed for replicable results | |
random.seed(3210) | |
d = {'math': 99, 'english': 80, 'chemistry': 67, 'biology': 88, 'physics': 93} | |
dict(random.sample(d.items(), 2)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
f = 123.456789 | |
print("%.2f" % f) # 123.46 | |
print("{0:.3f}".format(f)) # 123.467 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pyspark.sql import SparkSession | |
import databricks.koalas as ks | |
import json | |
import pydoop.hdfs as hdfs | |
spark = SparkSession.builder.enableHiveSupport().getOrCreate() | |
my_data = {"name": ["John", "Mary", "Kevin"], | |
"area": ["London", "Munich", "Berlin"], | |
"age": [33, 56, 44]} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
df = pd.DataFrame(columns=['A','B','C','D','E','F','G']) | |
print(df) | |
""" | |
Empty DataFrame | |
Columns: [A, B, C, D, E, F, G] | |
Index: [] | |
""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
df = pd.DataFrame(columns=['A','B','C','D','E','F','G']) | |
# rows | |
print(len(df.index)) # 0 - slightly faster in large dfs | |
print(df.shape[0]) # 0 | |
# columns | |
print(len(df.columns)) # 7 |