'DataFrame' object has no attribute 'to_frame'

大城市里の小女人 提交于 2021-02-08 10:53:35

问题


I am new to python. Just following the tutorial: https://www.hackerearth.com/practice/machine-learning/machine-learning-projects/python-project/tutorial/

This is the dataframe miss:

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

Now I just want to visualize those missing values"

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

And this is the error I got:

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'

What am I missing here?


回答1:


Check print(type(miss)) it should be <class 'pandas.core.series.Series'>

You have is dataframe, somewhere in the code you are doing wrong.

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'

I traced the tutorial, and below is the order flow

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) converts in into pandas.core.series.Series from pandas.core.frame.DataFrame

You are probably messed code at this place.



来源:https://stackoverflow.com/questions/49566862/dataframe-object-has-no-attribute-to-frame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!