将python文件导入jupyter笔记本

Posted

技术标签:

【中文标题】将python文件导入jupyter笔记本【英文标题】:import python file into jupyter notebook 【发布时间】:2019-12-27 21:48:05 【问题描述】:

我有一个 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】:

这行得通:

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

【讨论】:

以上是关于将python文件导入jupyter笔记本的主要内容,如果未能解决你的问题,请参考以下文章

无法让熊猫打开 CSV [Python, Jupyter, Pandas]

Jupyter笔记本上的cv2导入错误

使用jupyter notebook时导入文件时出错

在 Jupyter 笔记本上导入 openCV 时出现问题,即使它安装在我的机器上

Jupyter Notebook从同一目录中的python文件导入类

如何将一个Jupyter笔记本导入另一个