Shiny
Shiny

Reputation: 67

Iteratively passing column names as arguments to plot() function pandas

I want to iteratively assign col names to the value s for each column name that I have. I have tried everything but cannot get through this. The following code throws an error:

for i in continuous_data.columns :
   plt.figure()
   continuous_data.plot(x="Lattitude", y="Longtitude", kind="scatter",alpha=0.4,figsize=(12,10),c="Price",colormap="gist_rainbow",s=continuous_data.i,colorbar=True)
   plt.legend()

The error is:

    ---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-8-2489dccddba7> in <module>()
      1 for i in continuous_data.columns :
      2     plt.figure()
----> 3     continuous_data.plot(x="Lattitude", y="Longtitude", kind="scatter", alpha=0.4,figsize=(12,10),c="Price",colormap="gist_rainbow",s=continuous_data.i,colorbar=True)
      4     plt.legend()

/opt/conda/lib/python3.6/site-packages/pandas/core/generic.py in __getattr__(self, name)
   3612             if name in self._info_axis:
   3613                 return self[name]
-> 3614             return object.__getattribute__(self, name)
   3615 
   3616     def __setattr__(self, name, value):

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

<matplotlib.figure.Figure at 0x7ff9c144aa58>

When I am doing the following, it is yielding correct column names though:

    for i in continuous_data.columns:
    print(i)
Rooms
Price
Distance
Postcode
Bedroom2
Bathroom
Car
BuildingArea
Landsize
Lattitude
Longtitude
Propertycount

Can you guys help me with this? Link to the kernel on Kaggle is:https://www.kaggle.com/shinydhar/melbourne-housing-analysis

Upvotes: 2

Views: 274

Answers (1)

Ami Tavory
Ami Tavory

Reputation: 76297

The problem is with

s=continuous_data.i

For you it's obvious that it means the column i over which you're iterating. For Pandas, thought, this is

s=continuous_data['i']

and your DataFrame apparently doesn't have such a column (and it's not what you mean anyway).

Simply replace this with

s=continuous_data[i]

Upvotes: 1

Related Questions