繁体   English   中英

将 python 文件导入 jupyter notebook

[英]import python file into 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'

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

这有效:

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