首页 > 解决方案 > pandas_profiling 主要方法无法正常工作...构造函数有效但方法无效

问题描述

df.profile_report() 在使用 import pandas_profiling 安装后立即失败

该软件包已正确安装,因为我可以通过仅导入和使用构造函数 ProfileReport(df) 在 Jupyter 中生成报告。但是,语法 df.profile_report() 不起作用。

当我运行 df.profile_report() 时,我收到以下错误消息:

```AttributeError Traceback (most recent call last)
in 
----> 1 df.profile_report()

C:\Anaconda3\envs\quantecon\lib\site-packages\pandas\core\generic.py in getattr(self, name)
5065 if self._info_axis._can_hold_identifiers_and_holds_name(name):
5066 return self[name]
-> 5067 return object.getattribute(self, name)
5068
5069 def setattr(self, name, value):

AttributeError: 'DataFrame' object has no attribute 'profile_report'
```

版本信息:Python 3.7.1 pandas==0.24.2

```import numpy as np
import pandas as pd
from pandas_profiling import ProfileReport

# The dataframe is the same as the tutorial example given by the author.  

df = pd.DataFrame(np.random.rand(100, 5), columns=['a', 'b', 'c', 'd', 'e'])    

df.profile_report() # this fails.```

我尝试过的其他工作如下: from pandas_profiling import ProfileReport ...steps to create dataframe df ProfileReport(df)

单独使用构造函数 ProfileReport(df) 至少可以在我的 Jupyter Notebook 中获得一份报告。因此,我知道该软件包已安装并且可以正常工作。但是,获取报告的 object.method() 路由不起作用。但是许多其他方法依赖于 object.method() 语法。

我无法使用 df.profile_report() 方法获得任何数据框。

```import numpy as np
import pandas as pd
from pandas_profiling import ProfileReport

# The dataframe is the same as the tutorial example given by the author.  

df = pd.DataFrame(
    np.random.rand(100, 5),
    columns=['a', 'b', 'c', 'd', 'e']
)    

df.profile_report() # this fails.
ProfileReport(df)  # this works, but `df.profile_report()` does not work.
```

我猜怎么了……?

由于 pandas 错误是指 Pandas Core DataFrame 的“generic.py”,而错误是“没有属性 'profile_report',可能是装饰器包装了 dataframe 对象并对其进行了修改以赋予其额外的属性方法 . profile_report() ?? 这是我的猜测。我不知道是什么导致了错误,因为它在我“偷看”并直接使用报告构造函数时起作用。我只是不能使用依赖对象的其他方法.method() 语法。

标签: pythonpandasdataframepandas-profiling

解决方案


.profile_report()语法是在pandas_profiling版本 2 中引入的。

您可以通过 pip: 安装此版本pip install pandas-profiling

编辑

导入包的方法是:

import pandas_profiling

与您当前的方法相反

from pandas_profiling import ProfileReport


推荐阅读