简体   繁体   English

将 python 文件导入 jupyter notebook

[英]import python file into jupyter notebook

I have a python file bucket.py.我有一个python文件bucket.py。 I'm trying to import it in to a jupyter notebook using the code below.我正在尝试使用以下代码将其导入 jupyter 笔记本。 I'm then trying to use one of the functions in it "exp1" to explore a dataframe.然后我尝试使用其中的一个函数“exp1”来探索数据帧。 I'm getting the error below.我收到以下错误。 Can someone please tell me how to import a file from a directory so I can use the functions in it, in my jupyter notebook?有人可以告诉我如何从目录中导入文件,以便我可以在我的 jupyter 笔记本中使用其中的功能吗?

code:代码:

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)

error:错误:

---------------------------------------------------------------------------
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 file:存储桶.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])

this worked:这有效:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM