Python

Data Wrangling in Python – How to Applying Operations Over pandas Dataframes

Applying Operations Over pandas Dataframes Import Modules import pandas as pd import numpy as np Create a dataframe data = {‘name’: [‘Jason’, ‘Molly’, ‘Tina’, ‘Jake’, ‘Amy’], ‘year’: [2012, 2012, 2013, 2014, 2014], ‘reports’: [4, 24, 31, 2, 3], ‘coverage’: [25, 94, 57, 62, 70]} df = pd.DataFrame(data, index = [‘Cochice’, ‘Pima’, ‘Santa Cruz’, ‘Maricopa’, ‘Yuma’]) …

Learn Python By Example – if and if else

if and if else Create a variable with the status of the conflict. 1 if the conflict is active 0 if the conflict is not active unknown if the status of the conflict is unknwon   conflict_active = 1 If the conflict is active print a statement if conflict_active == 1: print(‘The conflict is active.’) …

Learn Python By Example – Selecting Items In A List With Filters

Selecting Items In A List With Filters /* Create an list of items denoting the number of soldiers in each regiment, view the list */ regimentSize = (5345, 6436, 3453, 2352, 5212, 6232, 2124, 3425, 1200, 1000, 1211); regimentSize (5345, 6436, 3453, 2352, 5212, 6232, 2124, 3425, 1200, 1000, 1211) One-line Method This line of …

Learn Python By Example – Functions Vs. Generators

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 */ …

Learn Python By Example – Function Basics

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 …

Learn Python By Example – Formatting Numbers

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 */ …

Learn Python By Example – Flatten Lists Of Lists

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 …

Learn Python By Example – Exiting A Loop

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 …

Learn Python By Example – Display Scientific Notation As Floats

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) …

learn Python By Example – Dictionary Basics

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’] = …