'数据帧'对象没有属性' to_frame'

时间:2018-03-30 00:21:25

标签: python python-3.x pandas

我是python的新手。只需按照教程:https://www.hackerearth.com/practice/machine-learning/machine-learning-projects/python-project/tutorial/

这是数据帧未命中:

miss = train.isnull().sum()/len(train)
miss = miss[miss>0]
miss.sort_values(inplace = True)
miss

Electrical      0.000685
MasVnrType      0.005479
MasVnrArea      0.005479
BsmtQual        0.025342
BsmtCond        0.025342
BsmtFinType1    0.025342
BsmtExposure    0.026027
BsmtFinType2    0.026027
GarageCond      0.055479
GarageQual      0.055479
GarageFinish    0.055479
GarageType      0.055479
GarageYrBlt     0.055479
LotFrontage     0.177397
FireplaceQu     0.472603
Fence           0.807534
Alley           0.937671
MiscFeature     0.963014
PoolQC          0.995205
dtype: float64

现在我只想想象那些缺失的值"

#visualising missing values
miss = miss.to_frame()
miss.columns = ['count']
miss.index.names = ['Name']
miss['Name'] = miss.index

这就是我得到的错误:

AttributeError                            Traceback (most recent call last)
<ipython-input-42-cd3b25e8862a> in <module>()
      1 #visualising missing values
----> 2 miss = miss.to_frame()

    C:\Users\Username\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
       2742             if name in self._info_axis:
       2743                 return self[name]
    -> 2744             return object.__getattribute__(self, name)
       2745 
       2746     def __setattr__(self, name, value):

    AttributeError: 'DataFrame' object has no attribute 'to_frame'

我在这里缺少什么?

2 个答案:

答案 0 :(得分:1)

检查print(type(miss))它应该是<class 'pandas.core.series.Series'>

你有数据框,代码中某处你做错了。

df = pd.DataFrame()
df.to_frame()
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Users\UR_NAME\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\generic.py", line 3614, in __getattr__
    return object.__getattribute__(self, name)
AttributeError: 'DataFrame' object has no attribute 'to_frame'

我追踪了教程,下面是订单流

train = pd.read_csv("train.csv")
print(type(train)) # <class 'pandas.core.frame.DataFrame'>
miss = train.isnull().sum()/len(train)
print(type(miss)) # <class 'pandas.core.series.Series'>

miss = train.isnull().sum()/len(train)pandas.core.series.Series

转换为pandas.core.frame.DataFrame

你可能在这个地方乱码。

答案 1 :(得分:0)

如果在当前单元格运行时使用 Notebook,“miss”将转换为数据框,以便第一次显示输出。如果您再次运行该单元格,您将收到一个/错误,因为它已经是一个数据框。所以再次运行前一个单元格,然后运行当前单元格来解决问题。笔记本本身就是这样工作的。