将python文件导入jupyter笔记本

时间:2019-12-27 21:48:05

标签: python-3.x jupyter-notebook

我有一个python文件bucket.py。我正在尝试使用下面的代码将其导入到jupyter笔记本中。然后,我尝试使用“ exp1”中的功能之一来探索数据框。我收到下面的错误。有人可以告诉我如何从目录中导入文件,以便在jupyter笔记本中使用其中的功能吗?

代码:

import importlib.util
spec = importlib.util.spec_from_file_location("module.name", '/Users/stuff/bucket/bucket.py')
foo = importlib.util.module_from_spec(spec)


foo.exp1(df)

错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-9-e1cc80f06e24> in <module>
----> 1 foo.exp1(harborsideoakland_df)

AttributeError: module 'module.name' has no attribute 'exp1'

bucket.py文件:

# import libraries

import numpy as np
import pandas as pd
from time import time
import scipy.stats as stats

from IPython.display import display # Allows the use of display() for DataFrames

# # Pretty display for notebooks
# %matplotlib inline

###########################################
# Suppress matplotlib user warnings
# Necessary for newer version of matplotlib
import warnings
warnings.filterwarnings("ignore", category = UserWarning, module = "matplotlib")
#
# Display inline matplotlib plots with IPython
from IPython import get_ipython
get_ipython().run_line_magic('matplotlib', 'inline')
###########################################

import matplotlib.pyplot as plt
import matplotlib.cm as cm

import warnings
warnings.filterwarnings('ignore')

import seaborn as sns

from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score
from sklearn.preprocessing import MinMaxScaler
from sklearn.decomposition import PCA








### HELPER FUNCTIONS:

# Initial Exploration



def exp1(df):

    with pd.option_context('display.max_rows', None, 'display.max_columns', None):
        # shape of data

        print('rows and columns: {}'.format(df.shape))

        # head data

        # display(df.head())
        print('')
        # data types and columns in data
        print('data types and columns in data:')
        print('')
        #display(df.info())
        print(df.info())
        print('')
        # unique values in each column
        print('unique values in each column:')
        #display(df.nunique())
        print(df.nunique())
        print('')
        # percentage duplicates
        print('percentage duplicates : {}'.format(1-(float(df.drop_duplicates().shape[0]))/df.shape[0]))
        print('')
        ## Percentage of column with missing values
        print('Percentage of column with missing values:')
        print('')
        missingdf=df.apply(lambda x: float(sum(x.isnull()))/len(x))

        #display(missingdf.head(n=missingdf.shape[0]))
        print(missingdf.head(n=missingdf.shape[0]))
        print('')
        print('Data snapshot:')
        print('')

        print(df[:5])

1 个答案:

答案 0 :(得分:0)

这有效:

import sys
sys.path.append(r'/Users/stuff/bucket/bucket')
import bucket as Lb