Month: May 2021

Data Wrangling in Python – How to Convert A Variable To A Time Variable In pandas

Convert A Variable To A Time Variable In pandas /* Import Preliminaries */ import pandas as pd /* Create a dataset with the index being a set of names */ raw_data = {‘date’: [‘2014-06-01T01:21:38.004053’, ‘2014-06-02T01:21:38.004053’, ‘2014-06-03T01:21:38.004053’], ‘score’: [25, 94, 57]} df = pd.DataFrame(raw_data, columns = [‘date’, ‘score’]) df date score 0 2014-06-01T01:21:38.004053 25 1 2014-06-02T01:21:38.004053 …

Data Wrangling in Python – How to Convert A String Categorical Variable To A Numeric Variable

Convert A String Categorical Variable To A Numeric Variable import modules import pandas as pd Create dataframe raw_data = {‘patient’: [1, 1, 1, 2, 2], ‘obs’: [1, 2, 3, 1, 2], ‘treatment’: [0, 1, 0, 1, 0], ‘score’: [‘strong’, ‘weak’, ‘normal’, ‘weak’, ‘strong’]} df = pd.DataFrame(raw_data, columns = [‘patient’, ‘obs’, ‘treatment’, ‘score’]) df patient obs …

Learn Python By Example – How to Convert A Categorical Variable Into Dummy Variables

Convert A Categorical Variable Into Dummy Variables /* import modules */ import pandas as pd /* Create a dataframe */ raw_data = {‘first_name’: [‘Jason’, ‘Molly’, ‘Tina’, ‘Jake’, ‘Amy’], ‘last_name’: [‘Miller’, ‘Jacobson’, ‘Ali’, ‘Milner’, ‘Cooze’], ‘sex’: [‘male’, ‘female’, ‘male’, ‘female’, ‘female’]} df = pd.DataFrame(raw_data, columns = [‘first_name’, ‘last_name’, ‘sex’]) df first_name last_name sex 0 Jason Miller …

Data Wrangling in Python – How to Construct A Dictionary From Multiple Lists

Construct A Dictionary From Multiple Lists Create Two Lists /* Create a list of the officer’s name */ officer_names = [‘Sodoni Dogla’, ‘Chris Jefferson’, ‘Jessica Billars’, ‘Michael Mulligan’, ‘Steven Johnson’] /* Create a list of the officer’s army */ officer_armies = [‘Purple Army’, ‘Orange Army’, ‘Green Army’, ‘Red Army’, ‘Blue Army’] Construct A Dictionary From …

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 …