繁体   English   中英

无法识别 Python 导入

[英]Python imports not being recognised

我一直在将我编写的一组函数放入更有条理的格式中,以便在需要时可以导入它们,但看起来我的结构不正确,需要一些建议。

目前我有文件UsefulFunctions.py

在文件的顶部,我整理了导入语句,例如import pandas as pd

在此之下,我创建了单独的类来保存类似的功能。 每个函数都是一个类中的静态方法,下面是一个用于解释问题的摘录:

有用的函数.py

import pandas as pd

class Pandas_functions:

    @staticmethod
    def null_values(dataframe):
        """
        Returns a dataframe of the count of null and N/A values per column of
        the input dataframe
        
        Parameters:
        -----------
        dataframe: a pandas dataframe
        
        Returns:
        --------
        A pandas dataframe listing all column features and the sum of Null & Empty
        values per feature
        """

        a = dataframe.isnull().astype(int).sum()
        b = (dataframe=="").astype(int).sum()
        result = pd.DataFrame(data=[a,b], index=['Null','Empty']).T
        
        return result

当我然后在一个单独的笔记本中import UsefulFunction并尝试使用UsefulFunctions.Pandas_functions.null_values(df)我收到以下错误:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-30-7c232dd49b55> in <module>
----> 1 UsefulFunctions.Pandas_functions.null_values(df_docks)

~\Synoptic Project\UsefulFunctions.py in null_values(dataframe)
    190         --------
    191         A pandas dataframe listing all column features and the sum of Null & Empty
--> 192         values per feature
    193         """
    194 

NameError: name 'pd' is not defined

​

很明显,pd 是在 UsefFunctions.py 中定义的,我已经将它导入到我正在处理的笔记本中。 所以,我不确定我在这里缺少什么以使该功能正常工作。

任何人都可以请建议我应该如何做到这一点?

谢谢

您可能在文件开头遗漏了此语句

在此处输入图片说明

暂无
暂无

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

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