Gists
This a growing collection of small snippets of code that I use frequently, and otherwise would have been stored in my Gists on GitHub.
Jupyter
Setting up Jupyter Kernel for a Virtual Environment
# After activating virtual environment
pip install ipykernel
ipython kernel install --user --name="my-new-venv" --display-name="my-new-venv"
List and Uninstall Jupyter Kernel
jupyter kernelspec list
jupyter kernelspec uninstall XXXX
Plotting
Removing Matplotlib figure transparency (in Jupyter Lab)
If you try to grab a figure directly from a Jupyter notebook without saving to a file first, the figure axis is transparent, which is not always desired.
= plt.gcf()
fig "white")
fig.patch.set_facecolor(1) fig.patch.set_alpha(
Temporarily change Seaborn plotting context with context manager
Seaborn has useful plotting context settings (talk
, poster
, paper
, notebook
) that you can permanently set for a session:
"talk") sns.set_context(
but you can also temporarily set a context for a particular plot that may differ than your other plots (talk
vs paper
)
with sns.plotting_context("paper"):
=df, x="x", y="y") sns.swarmplot(data
Pandas
Add column to dataframe and then return dataframe
You can add a new column to a dataframe (and set the values of the new column):
"new_col"] = "A" df[
But if you need to return a dataframe (in chaining or list comprehension), you can use the .assign()
method. The name of the new column is used as an argument with the method.
= df.assign(new_col="A") df
Git
Start Git Repo with Empty Commit
This is useful in case you ever need to rebase your repo back to the beginning, because it’s bothersome to deal with a root commit.
git init
git commit --allow-empty -m "Initial commit"