Skip to content

Instantly share code, notes, and snippets.

View akanik's full-sized avatar

Alexandra Kanik akanik

View GitHub Profile
@akanik
akanik / pandas-header-into-row.py
Created March 21, 2023 18:39
Pandas make header into row in dataframe
df.loc[len(df.index)] = list(df.columns)
#create two new columns via attribute table
#via: https://gis.stackexchange.com/questions/45243/how-to-determine-the-centroid-of-polygons
long = toreal(regexp_substr(geom_to_wkt(centroid($geometry)), '(-?\\d+\\.?\\d*) -?\\d+\\.?\\d*'))
lat = toreal(regexp_substr(geom_to_wkt(centroid($geometry)), '-?\\d+\\.?\\d* (-?\\d+\\.?\\d*)'))
data_dir10 = '../data/2010/tx2010.pl/'
FILES10 = {
'cols': '../data/2010/2010_PLSummaryFile_FieldNames.xlsx',
'geoheader': data_dir10+'txgeo2010.pl',
'seq01': data_dir10+'tx000012010.pl',
'seq02': data_dir10+'tx000022010.pl',
}
def return_dtypes_dict(df):
@akanik
akanik / qgis-int-string
Created July 21, 2021 16:06
qgis convert int to string field calculator
right( ('000' || tostring( "myfield" )), 4)
@akanik
akanik / compress-geoTIFF
Created July 6, 2021 18:58
Compress geoTIFF
# from: https://github.com/cogeotiff/cog-spec/blob/master/spec.md
# You need to install GDAL first
# Run this on the command line, replace in.tif and out.tif with your desired file names
gdal_translate in.tif out.tif -co TILED=YES -co COPY_SRC_OVERVIEWS=YES -co COMPRESS=LZW
@akanik
akanik / pandas-chart
Created January 26, 2021 22:12
template for those pandas charts you love so much
import pandas as pd
import numpy as np
import glob
import matplotlib.pyplot as plt
import re
def linechart(df, x_dim, y_dim, xaxis, yaxis, title):
fig, ax = plt.subplots(figsize=(15, 7))
if x_dim == 'index':
virtualenv -p python3 envname
basepath = '../data/restaurant-violations/csv/'
df_list = []
for filename in os.listdir(basepath):
filepath = os.path.join(basepath, filename)
print(filepath)
if filename.endswith('.csv'):
df = pd.read_csv(filepath)
df_list.append(df)
#R
filepath <- 'path/to/folder'
file.rename(dir(filepath, full.names = TRUE), tolower(dir(filepath, full.names = TRUE)))
# If your working directory is set to the folder you don't need `full.names = TRUE`
# But it's best to not change your working directory
@akanik
akanik / python-lowercase-filenames
Created March 7, 2019 01:18
Lowercase and slugify filenames
# works with python3 and python2
from slugify import slugify
import os
directory = 'all-lower/'
for old_filename in os.listdir(directory):
old_filename_pieces = os.path.splitext(old_filename)