If Else On Any Or All Elements Preliminaries /* import pandas as pd */ import pandas as pd Create a simulated dataset /* Create an example dataframe */ data = {‘score’: [1,2,3,4,5]} df = pd.DataFrame(data) df score 0 1 1 2 2 3 3 4 4 5 Does any cell equal 3? /* If any …
Day: May 27, 2021
How To Use Default Dictionary Preliminaries import collections Create A DefaultDict Default Dicts work just like regular dictionaries, except a key is called that doesn’t have a value, a default value (note: value, not key) is supplied. /* Create a defaultdict with the default value of 0 (int’s default value is 0) */ arrests = …
Generating Random Numbers With NumPy Import Numpy import numpy as np Generate A Random Number From The Normal Distribution np.random.normal() 0.5661104974399703 Generate Four Random Numbers From The Normal Distribution np.random.normal(size=4) array([-1.03175853, 1.2867365 , -0.23560103, -1.05225393]) Generate Four Random Numbers From The Uniform Distribution np.random.uniform(size=4) array([ 0.00193123, 0.51932356, 0.87656884, 0.33684494]) Generate Four Random Integers Between 1 …
Functions Vs. Generators Create A Function /* Create a function that */ def function(names): /* For each name in a list of names */ for name in names: /* Returns the name */ return name /* Create a variable of that function */ students = function([‘Abe’, ‘Bob’, ‘Christina’, ‘Derek’, ‘Eleanor’]) /* Run the function */ …
Function Basics Create Function Called print_max def print_max(x, y): /* if a is larger than b */ if x > y: print(x, ‘is maximum’) /* if a is equal to b */ elif x == y: print(x, ‘is equal to’, y) /* otherwise */ else: print(y, ‘is maximum’) Run Function With Two Arguments print_max(3,4) 4 …
Formatting Numbers Create A Long Number annual_revenue = 9282904.9282872782 Format Number /* Format rounded to two decimal places */ format(annual_revenue, ‘0.2f’) ‘9282904.93’ /* Format with commas and rounded to one decimal place */ format(annual_revenue, ‘0,.1f’) ‘9,282,904.9’ /* Format as scientific notation */ format(annual_revenue, ‘e’) ‘9.282905e+06’ /* Format as scientific notation rounded to two deciminals */ …
Flatten Lists Of Lists Create A List Of Lists /* Create a list containing three lists of names */ list_of_lists = [[‘Amy’,’Betty’,’Cathryn’,’Dana’], [‘Elizabeth’,’Fay’,’Gora’], [‘Heidi’,’Jane’,’Kayley’]] Flatten The Lists Of Lists Into A Single List /* For each element in list_of_lists, take each element in the list */ flattened_list = [i for row in list_of_lists for i …
Exiting A Loop Create A List /* Create a list: */ armies = [‘Red Army’, ‘Blue Army’, ‘Green Army’] Breaking Out Of A For Loop for army in armies: print(army) if army == ‘Blue Army’: print(‘Blue Army Found! Stopping.’) break Red Army Blue Army Blue Army Found! Stopping. Notice that the loop stopped after the …
Display Scientific Notation As Floats Create Values /* Create a numbers in scientific notation */ value_scientific_notation = 6.32000000e-03 /* Create a vector of numbers in scientific notation */ vector_scientific_notation = [6.32000000e-03, 1.80000000e+01, 2.31000000e+00, 0.00000000e+00, 5.38000000e-01, 6.57500000e+00, 6.52000000e+01, 4.09000000e+00, 1.00000000e+00, 2.96000000e+02, 1.53000000e+01, 3.96900000e+02, 4.98000000e+00] Display Values As Floats /* Display value as a float */ ‘{:f}’.format(value_scientific_notation) …
Dictionary Basics Basics Not sequences, but mappings. That is, stored by key, not relative position. Dictionaries are mutable. Build a dictionary via brackets unef_org = {‘name’ : ‘UNEF’, ‘staff’ : 32, ‘url’ : ‘http://unef.org’} View the variable unef_org {‘name’: ‘UNEF’, ‘staff’: 32, ‘url’: ‘http://unef.org’} Build a dict via keys who_org = {} who_org[‘name’] = …