Python 将函数导入Jupyter笔记本时出现名称错误

Python 将函数导入Jupyter笔记本时出现名称错误,python,pandas,jupyter-notebook,Python,Pandas,Jupyter Notebook,我正在AWS Sagemaker实例上使用Jupyter笔记本。为了方便起见,我编写了一个.py文件,其中包含定义的两个函数 #function to gather the percent of acts in each label feature combo def compute_pct_accts(data, label_cnt): """ data is the output from aggregate_count labe_cnt gives the

我正在AWS Sagemaker实例上使用Jupyter笔记本。为了方便起见,我编写了一个.py文件,其中包含定义的两个函数

#function to gather the percent of acts in each label feature combo

def compute_pct_accts(data, label_cnt):
"""
data is the output from aggregate_count
labe_cnt gives the breakdown of data for each target value
"""

label_data_combined = pd.merge(data, label_cnt, how='inner', left_on= 'label', right_on = 'label')
label_data_combined['Act_percent'] = np.round((label_data_combined['ACT_CNT']/label_data_combined['Total_Cnt'])*100,2)
return label_data_combined


#write a function to perform aggregation for target and feature column

def aggregate_count(df, var, target):

"""
df is the dataframe,
var is the feature name
target is the label varaible(0 or 1)
"""

label_var_cnt = df.groupby([var,target],observed=True)['ID'].count()
label_var_cnt = label_var_cnt.reset_index()
label_var_cnt.rename(columns={'ID':'ACT_CNT'},inplace=True)
return label_var_cnt
这两个函数都存储在名为file1.py的.py文件中。然后,为了在我的笔记本上找到它们,我打字了

from file1 import *
import pandas as pd
此命令确实导入了这两个函数。但是当我尝试运行函数时

compute_pct_accts(GIACT_Match_label_cnt, label_cnt)

我得到一个名字错误


pd not found
请注意,我在jupyter笔记本中输入了熊猫作为pd。我知道使用该选项

%run -i compute_pct_accts_new.py

但这迫使我用这个函数编写一个新的python文件。我的问题是,我们能否拥有一个定义了所有函数的python文件,这样我们就可以一次导入所有函数并在笔记本中交互使用。
非常感谢您的帮助。

请尝试在包含要导入的函数的.py文件中导入熊猫。

感谢您的回复。我按照建议在python文件中添加了import pandas作为pd。但错误仍然是相同的名称pd未定义。请查看此帖子:hhttps://stackoverflow.com/a/54725559 也许您可以创建一个包含所需功能的模块,并将其安装到您的环境中。然后,您将从上述模块中导入它们。不,您还需要在file1.py中导入pandas。