Skip to content

Instantly share code, notes, and snippets.

View LeonardAukea's full-sized avatar
🕵️‍♂️
Alive

Leonard Aukea LeonardAukea

🕵️‍♂️
Alive
View GitHub Profile
@LeonardAukea
LeonardAukea / geolocate_ip.py
Last active September 12, 2018 20:03
geolocate w/ ip address #python
import requests
import json
def geolocate_ip(ip):
'''Get location data based on ip.
Note: Limited (Free) to a 1000 requests per day.
TODO(Leonard): Handler for being behind a vpn
Inputs:
@LeonardAukea
LeonardAukea / parse_tuple.py
Created August 13, 2018 12:42
Python: parse string as tuple
def parse_tuple(string):
'''Parse a string like '(23.0,11.3)' to tuple of floats.'''
try:
s = eval(str(string))
if type(s) == tuple:
return s
return
except:
return
@LeonardAukea
LeonardAukea / togeodf.py
Created August 7, 2018 12:30
Converts pandas dataframe with coordinates to geopandas dataframe.
from geopandas import GeoDataFrame
from shapely.geometry import Point
def to_gdf(df, lon, lat, crs={'init': 'epsg:4326'}):
'''Converts a pandas dataframe with coordinate columns to a geopandas dataframe.
Inputs:
df: pandas dataframe
lon: str; name of longitude column
lat: str; name of Latitude column
@LeonardAukea
LeonardAukea / stacked-barchart.py
Last active September 18, 2018 18:16
Stacked barchart
def overlapped_bar(df, show=False, width=0.9, alpha=.5,
title='', xlabel='', ylabel='', **plot_kwargs):
"""Like a stacked bar chart except bars on top of each other with transparency"""
xlabel = xlabel or df.index.name
N = len(df)
M = len(df.columns)
indices = np.arange(N)
colors = ['steelblue', 'firebrick', 'darksage', 'goldenrod', 'gray'] * int(M / 5. + 1)
for i, label, color in zip(range(M), df.columns, colors):
kwargs = plot_kwargs
@LeonardAukea
LeonardAukea / remove_third_dimension.py
Last active August 13, 2018 12:43
Python: Shapely Remove third dimension
from shapely.geometry import *
def remove_third_dimension(geom):
if geom.is_empty:
return geom
if isinstance(geom, Polygon):
exterior = geom.exterior
new_exterior = remove_third_dimension(exterior)
@LeonardAukea
LeonardAukea / dualExplode.py
Created November 15, 2017 00:37
Pyspark: dual explode generator
def dualExplode(row):
"""Explode weights and category_ids list elements to separate rows.
Args:
row: Row
Yield:
Row(**newDict)
"""
rowDict = row.asDict()
xList = rowDict.pop('x')
yList = rowDict.pop('y')