{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Hoisting dataframes to the module.\n", "\n", "Creating modules from data is different than software\n", "In practice, I really like to tidy up dataframes with specific features cleaned in each notebook. This approach avoids long and difficult to debug notebooks." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from pandas import util; df = util.testing.makeDataFrame()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "All of the columns in the namespace" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "globals().update(dict(df.items()))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Add dataframe operations to our module" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import IPython" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Hosting attributes." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "with IPython.utils.capture.capture_output(): globals().update({x: getattr(df, x) for x in dir(df)})\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "Ø = __name__ == '__main__'\n", "if Ø: from deathbeds import __Dataframes_and_modules" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ABCD
QyXyHT1cgE2.3956130.1897970.2475240.756322
CpKDTadz2K-0.667076-1.6094361.048942-0.031083
\n", "
" ], "text/plain": [ " A B C D\n", "QyXyHT1cgE 2.395613 0.189797 0.247524 0.756322\n", "CpKDTadz2K -0.667076 -1.609436 1.048942 -0.031083" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "if Ø: assert isinstance(__Dataframes_and_modules, __import__('types').ModuleType)\n", "Ø and __Dataframes_and_modules.sample(2)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.6" } }, "nbformat": 4, "nbformat_minor": 2 }