Month: May 2021

Data Wrangling in Python – How to Break Up A String Into Columns Using Regex In pandas

Breaking Up A String Into Columns Using Regex In pandas Import modules import re import pandas as pd Create a dataframe of raw strings /* Create a dataframe with a single column of strings */ data = {‘raw’: [‘Arizona 1 2014-12-23 3242.0’, ‘Iowa 1 2010-02-23 3453.7’, ‘Oregon 0 2014-06-20 2123.0’, ‘Maryland 0 2014-03-14 1123.6’, ‘Florida …

Data Wrangling in Python – How to Break A List Into N-Sized Chunks

Break A List Into N-Sized Chunks In this snippet we take a list and break it up into n-size chunks. This is a very common practice when dealing with APIs that have a maximum request size.   /* Create a list of first names */ first_names = [‘Steve’, ‘Jane’, ‘Sara’, ‘Mary’,’Jack’,’Bob’, ‘Bily’, ‘Boni’, ‘Chris’,’Sori’, ‘Will’, …

Data Wrangling in Python – How to Assign A New Column To A Pandas DataFrame

Assign A New Column To A Pandas DataFrame Preliminaries import pandas as pd Create Dataframe /* Create empty dataframe */ df = pd.DataFrame() /* Create a column */ df[‘name’] = [‘John’, ‘Steve’, ‘Sarah’] /* View dataframe */ df name 0 John 1 Steve 2 Sarah Assign New Column To Dataframe /* Assign a new column …

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

Data Wrangling in Python – How to Apply Operations To Groups In Pandas

Apply Operations To Groups In Pandas Preliminaries /* import modules */ import pandas as pd /* Create dataframe */ raw_data = {‘regiment’: [‘Nighthawks’, ‘Nighthawks’, ‘Nighthawks’, ‘Nighthawks’, ‘Dragoons’, ‘Dragoons’, ‘Dragoons’, ‘Dragoons’, ‘Scouts’, ‘Scouts’, ‘Scouts’, ‘Scouts’], ‘company’: [‘1st’, ‘1st’, ‘2nd’, ‘2nd’, ‘1st’, ‘1st’, ‘2nd’, ‘2nd’,’1st’, ‘1st’, ‘2nd’, ‘2nd’], ‘name’: [‘Miller’, ‘Jacobson’, ‘Ali’, ‘Milner’, ‘Cooze’, ‘Jacon’, ‘Ryaner’, ‘Sone’, …

Data Wrangling in Python – How to Apply Functions By Group In Pandas

Apply Functions By Group In Pandas Preliminaries import pandas as pd Create a simulated dataset /* Create an example dataframe */ data = {‘Platoon’: [‘A’,’A’,’A’,’A’,’A’,’A’,’B’,’B’,’B’,’B’,’B’,’C’,’C’,’C’,’C’,’C’], ‘Casualties’: [1,4,5,7,5,5,6,1,4,5,6,7,4,6,4,6]} df = pd.DataFrame(data) df Casualties Platoon 0 1 A 1 4 A 2 5 A 3 7 A 4 5 A 5 5 A 6 6 B 7 …

Learn Python By Example – while Statement

while Statement Import the random module import random Create a variable of the true number of deaths of an event deaths = 6 Create a variable that is denotes if the while loop should keep running running = True while running is True while running: /* Create a variable that randomly create a integer between …

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 – Add Padding Around String

Try, Except, and Finally Create data /* Create some data */ scores = [23,453,54,235,74,234] Try something that doesn’t work /* Try to: */ try: /* Add a list of integers and a string */ scores + ‘A string of characters.’ /* If you get an error, set the error as ‘e’, */ except Exception as …

Learn Python By Example – Swapping Variable Values

Swapping Variable Values Setup the originally variables and their values one = 1 two = 2 View the original variables ‘one =’, one, ‘two =’, two (‘one =’, 1, ‘two =’, 2) Swap the values one, two = two, one View the swapped values, notice how the values for each variable have changed ‘one =’, …

learn Python By Example – String Operations

String Operations Python 3 has three string types str() is for unicode bytes() is for binary data bytesarray() mutable variable of bytes   Create some simulated text. string = ‘The quick brown fox jumped over the lazy brown bear.’ Capitalize the first letter. string_capitalized = string.capitalize() string_capitalized ‘The quick brown fox jumped over the lazy …

Learn Python By Example – String Indexing

String Indexing Create a string string = ‘Strings are defined as ordered collections of characters.’ Print the entire string string[:] ‘Strings are defined as ordered collections of characters.’ Print the first three characters string[0:3] ‘Str’ Print the first three characters string[:3] ‘Str’ Print the last three characters string[-3:] ‘rs.’ Print the third to fifth character …