Skip to content

Instantly share code, notes, and snippets.

View Per48edjes's full-sized avatar
👉
This is a good point.

Ravi Dayabhai Per48edjes

👉
This is a good point.
View GitHub Profile
@Per48edjes
Per48edjes / slice.py
Created September 30, 2017 03:04
Comparing in-memory locations of lists vs. slices of list vs. reassigning elements to list reference
x = [1,2,3]
def test():
print("x-slice id before",id(x[:]))
print("x-slice before", x[:])
x[:] += [4]
print("x-slice id after",id(x[:]))
print("x-slice after", x[:])
print("x id", id(x))
@Per48edjes
Per48edjes / mergesort.py
Created January 6, 2018 03:37
Simple Mergesort
#!/usr/bin/python
def merge(l):
# Base case
if len(l) < 2:
return l
mid = len(l) // 2
y = merge(l[:mid])
@Per48edjes
Per48edjes / hide_cells
Last active November 11, 2018 04:27
Toggle code cell visibility in Jupyter Notebook
from IPython.display import HTML
HTML('''<script>
code_show=true;
function code_toggle() {
if (code_show){
$('div.input').hide();
} else {
$('div.input').show();
}
@Per48edjes
Per48edjes / ColumnTransform_pipeline_field_extraction.py
Created November 13, 2019 22:44
Get column names from ColumnTransformer with embedded pipelines (SKLearn)
## Split into holdout for purposes of imputation and encoding
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = .2, random_state=2019)
class_labels = df['segment_label'].cat.categories
## Preprocessing pipeline
# Define transforms on numeric types
numeric_features = X.select_dtypes(np.number).columns
numeric_transformer = Pipeline(steps=[
('imputer', SimpleImputer(strategy='median')),
@Per48edjes
Per48edjes / stacked_bar_chart.py
Created December 25, 2019 00:55
Function to plot a stacked bar chart
import numpy as np
import matplotlib.pyplot as plt
def plot_stacked_bar(data, series_labels, category_labels=None,
show_values=False, value_format="{}", y_label=None,
colors=None, grid=True, reverse=False):
"""Plots a stacked bar chart with the data and labels provided.
Keyword arguments:
data -- 2-dimensional numpy array or nested list
@Per48edjes
Per48edjes / pandas_dataframe_difference.py
Created April 23, 2020 01:15 — forked from toddbirchard/pandas_dataframe_difference.py
Helper function to compare two DataFrames and find rows which are unique or shared.
def dataframe_difference(df1, df2, which=None):
"""Find rows which are different."""
comparison_df = df1.merge(df2,
indicator=True,
how='outer')
if which is None:
diff_df = comparison_df[comparison_df['_merge'] != 'both']
else:
diff_df = comparison_df[comparison_df['_merge'] == which]
diff_df.to_csv('data/diff.csv')
@Per48edjes
Per48edjes / dupe_checking_paradigm.py
Last active June 21, 2020 04:21
Dupe-check logging
import datetime
# Filename function for logging
def dt_filename(
filename: str,
extension: str,
path: str = "",
date: str = str(datetime.datetime.now()),
) -> str:
filename = "_".join([filename, date])
@Per48edjes
Per48edjes / substring_list_match.py
Created July 2, 2020 11:52
Multiple substring search among list of strings
def substring_list_match(string_list, substr_list):
return [target_str for target_str in string_list if any(search_str in target_str for search_str in substr_list)]
@Per48edjes
Per48edjes / df_side_by_side.py
Created September 30, 2020 19:03
Display dataframes in a row with captions
from IPython.core.display import display, HTML
def display_side_by_side(dfs:list, captions:list):
"""Display tables side by side to save vertical space
Input:
dfs: list of pandas.DataFrame
captions: list of table captions
"""
output = ""
combined = dict(zip(captions, dfs))
@Per48edjes
Per48edjes / git-pushing-multiple.rst
Created November 13, 2020 17:00 — forked from rvl/git-pushing-multiple.rst
How to push to multiple git remotes at once. Useful if you keep mirrors of your repo.

Pushing to Multiple Git Repos

If a project has to have multiple git repos (e.g. Bitbucket and Github) then it's better that they remain in sync.

Usually this would involve pushing each branch to each repo in turn, but actually Git allows pushing to multiple repos in one go.

If in doubt about what git is doing when you run these commands, just