Skip to content

Instantly share code, notes, and snippets.

# https://docs.microsoft.com/en-us/python/api/azure-cosmos/azure.cosmos.cosmos_client.cosmosclient?view=azure-python#upsertitem-database-or-container-link--document--options-none-
# Write rows of a pandas DataFrame as items to the Database Container
# Create Connection Link string
collection_link = database_link + '/colls/' + 'HDIcontainer'
# Write rows of a pandas DataFrame as items to the Database Container
for i in range(0,df.shape[0]):
# create a dictionary for the selected row
data_dict = dict(df.iloc[i,:])
# Initialize list
dflist = []
# Connection link
collection_link = database_link + '/colls/' + 'HDIcontainer'
# Write out query
query = 'SELECT * FROM c where c.country="Afghanistan" and c.level="National"'
# For-loop to retrieve individual json records from Cosmos DB
# that satisfy our query
for item in client.QueryItems(collection_link,
import seaborn as sns
import matplotlib.pyplot as plt
# Convert to type float
index_df = df.loc[:,('healthindex','incindex','edindex','year')].astype('float')
# Create the figure
plt.figure(figsize=(10,6))
# Set a Seaborn chart style
sns.set_style('darkgrid')
# Import packages
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# Indedependent variable - number of chimpanzees in hunting party
x = np.array([1,2,3,4,5,6,7,8])
# Dependent Variable - percent of successful hunts
y = np.array([30,45,51,57,60,65,70,71])
import pandas as pd
import blspandas
import api_key
import requests
import json
# Pull a list of state fips Ids.
bls_fips = blspandas.get_state_fips()
# Create a dictionary of BLS query Ids and States
def clean_bls_data(df):
'''
Function for cleaning BLS data into a 'tidy' vertical dataset like:
State | Date | Metric
Returns pandas dataframe.
'''
# Melt the dataframe into vertical format.
melted_df = pd.melt(df, id_vars=['Date'],
value_vars=df.columns[1:],
# Convert the Date column back to string,
# find January 1st 2020,
# only keep State and percent unemployed columns
# Set index to State
jan_emp = df[df['Date'].dt.strftime(date_format='%Y-%m-%d').str.contains('2020-01-01')][['State','Pct_Unemployed']].set_index('State')
# Convert the Date column back to string,
# find June 1st 2020,
# only keep State and percent unemployed columns
# Set index to State
from opencensus.ext.azure.log_exporter import AzureLogHandler
# function to initialize App Insights logger object
def startLogger():
from opencensus.ext.azure.log_exporter import AzureLogHandler
# Get Azure Monitor instrumentation key from environment variable
instrumentation_key = os.environ.get('InstrumentationKey')
# initialize logger object
logger = logging.getLogger(__name__)
import ipinfo
def ipData(address):
# https://stackoverflow.com/questions/24678308/how-to-find-location-with-ip-address-in-python
ip_address = address[0]
# ipinfo package - free up to 50k requests
# https://github.com/ipinfo/python
# login to ipinfo account: https://ipinfo.io/account?welcome=true
access_token = '<your-access-token'
# Write connection attempts to App Insights
# https://docs.microsoft.com/en-us/azure/azure-monitor/app/opencensus-python
def writeAppInsights(logger,address,data):
# create the custom_dimensions dictionary
logData = {'custom_dimensions': {
'Time': time.ctime(),
'IP': address[0],
'Port': address[1],