Python for Data Analyst

Data Wrangling in Python – How to List Unique Values In A pandas Column

List Unique Values In A pandas Column Special thanks to Bob Haffner for pointing out a better way of doing it. Preliminaries /* Import modules */ import pandas as pd /* Set ipython’s max row display */ pd.set_option(‘display.max_row’, 1000) /* Set iPython’s max column width to 50 */ pd.set_option(‘display.max_columns’, 50) Create an example dataframe /* …

Data Wrangling in Python – How to Join And Merge Pandas Dataframe

Join And Merge Pandas Dataframe import modules import pandas as pd from IPython.display import display from IPython.display import Image Create a dataframe raw_data = { ‘subject_id’: [‘1’, ‘2’, ‘3’, ‘4’, ‘5’], ‘first_name’: [‘Alex’, ‘Amy’, ‘Allen’, ‘Alice’, ‘Ayoung’], ‘last_name’: [‘Anderson’, ‘Ackerman’, ‘Ali’, ‘Aoni’, ‘Atiches’]} df_a = pd.DataFrame(raw_data, columns = [‘subject_id’, ‘first_name’, ‘last_name’]) df_a subject_id first_name last_name …

Data Wrangling in Python – How to Find Hierarchical Data In pandas

Hierarchical Data In pandas /* 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’, ‘Sloan’, ‘Piger’, ‘Riani’, …

Data Wrangling in Python – How to Create A pandas Column With A For Loop

Grouping Rows In pandas /* Import modules */ import pandas as pd /* Example 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’, ‘Sloan’, ‘Piger’, ‘Riani’, …

Data Wrangling in Python – How to Group Pandas Data By Hour Of The Day

Group Pandas Data By Hour Of The Day Preliminaries /* Import libraries */ import pandas as pd import numpy as np Create Data /* Create a time series of 2000 elements, one very five minutes starting on 1/1/2000 */ time = pd.date_range(‘1/1/2000′, periods=2000, freq=’5min’) /* Create a pandas series with a random values between 0 …

Data Wrangling in Python – How to Group Data By Time

Group Data By Time Preliminaries /* Import required packages */ import pandas as pd import datetime import numpy as np Next, let’s create some sample data that we can group by time as an sample. In this example I am creating a dataframe with two columns with 365 rows. One column is a date, the …

Data Wrangling in Python – How to Group A Time Series With pandas

Group A Time Series With pandas Import required modules import pandas as pd import numpy as np Create a dataframe df = pd.DataFrame() df[‘german_army’] = np.random.randint(low=20000, high=30000, size=100) df[‘allied_army’] = np.random.randint(low=20000, high=40000, size=100) df.index = pd.date_range(‘1/1/2014′, periods=100, freq=’H’) df.head() german_army allied_army 2014-01-01 00:00:00 21413 37604 2014-01-01 01:00:00 25913 21144 2014-01-01 02:00:00 22418 34201 2014-01-01 03:00:00 …

Data Wrangling in Python – How to Geolocate A City Or Country

Geolocate A City Or Country This tutorial creates a function that attempts to take a city and country and return its latitude and longitude. But when the city is unavailable (which is often be the case), the returns the latitude and longitude of the center of the country. Preliminaries from geopy.geocoders import Nominatim geolocator = …

Data Wrangling in Python – How to Geolocate A City And Country

Geolocate A City And Country This tutorial creates a function that attempts to take a city and country and return its latitude and longitude. But when the city is unavailable (which is often be the case), the returns the latitude and longitude of the center of the country. Preliminaries from geopy.geocoders import Nominatim geolocator = …

Data Wrangling in Python – How to Geocoding And Reverse Geocoding

Geocoding And Reverse Geocoding Geocoding (converting a physical address or location into latitude/longitude) and reverse geocoding (converting a lat/long to a physical address or location) are common tasks when working with geo-data. Python offers a number of packages to make the task incredibly easy. In the tutorial below, I use pygeocoder, a wrapper for Google’s …