Skip to content

Instantly share code, notes, and snippets.

@georgerichardson
georgerichardson / postgres_to_df.py
Last active May 9, 2018 08:23
PostgreSQL to DataFrame
import pandas as pd
from sqlalchemy import create_engine
from sqlalchemy.engine.url import URL
# db settings to make url (could be in external file)
db_settings = {'drivername': <database driver>, # 'postgres', 'mysql' etc.
'host': <host address>, # 'localhost' etc.
'username': <username>,
'password': <password>,
'database': <database name>
@georgerichardson
georgerichardson / txt_to_list.py
Last active July 25, 2017 21:28
Read a newline separated txt file into a list
with open('file.txt', 'r') as f:
text = f.read()
lines_list = text.split('\n')
set nocompatible " be iMproved, required
filetype off " required
" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')
" let Vundle manage Vundle, required
@georgerichardson
georgerichardson / timestamp_string.py
Created April 17, 2018 21:13
Adds timestamp to the end of string. Useful for file names.
from datetime import datetime
def timestamp_string(string, file_ext=True, hidden_file=False):
""" Adds a timestamp to the end of a string in the format
YYYY_MM_DD_hhmm.
Parameters:
string: str
string to be timestamped
file_ext: bool
@georgerichardson
georgerichardson / standardise_column_names.py
Created April 18, 2018 14:04
Make DataFrame column names lowercase and replace whitespace (and punctuation) with '_'
import string
def standardise_column_names(df, remove_punct=True):
""" Converts all DataFrame column names to lower case replacing
whitespace of any length with a single underscore. Can also strip
all punctuation from column names.
Parameters
----------
df: pandas.DataFrame
@georgerichardson
georgerichardson / group_merge.py
Created March 28, 2019 15:56
Group merging algorithm from Jyl
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 2 17:07:42 2019
@author: jdjumalieva
"""
import pandas as pd
import os
@georgerichardson
georgerichardson / 03_gr_sdg_verification.ipynb
Created May 26, 2020 16:13
sdg_cordis_verification.ipynb
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
import pandas as pd
import seaborn as sns
def order_by_correlation_cluster(data: pd.DataFrame, x: str, y: str, values: str) -> pd.DataFrame:
"""Takes a tidy dataframe, pivots it, and creates a clustermap based on
correlations between rows. Returns the original data with a new 'order'
column that contains the cardinal ordering of the y axis from the clustermap.
Made with plotting in altair in mind.
"""