1

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?

3
  • 1
    Please paste code, input, output, etc. as text, not as screenshots. Commented Mar 30, 2018 at 0:22
  • It is bad to post code or data as pictures. There is no way to copy and paste a picture into a code editor. So it will be much harder, and thus less likely, for someone to help. To get the most out of the site it is important to ask good questions, that includes creating a Minimal, Complete, and Verifiable example. Commented Mar 30, 2018 at 0:24
  • Fixed it. Thank you Commented Mar 30, 2018 at 0:40

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

0

If you use Notebook while the current cell is running, "miss" is converted to a data frame so that the output is displayed the first time. If you run the cell again, you will get an/the error because it is already a data frame. So run the previous cell again and then run the current cell to fix the problem. The notebook itself works this way.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.