Exploring Ebay Bar sales Data

Screen Link:

brand_mean_power_ps = {}

for brand in common_brands:
brand_only = autos[autos[“power_ps”] == brand]
mean_ps = autos[“power_ps”].mean()


/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/ops.py:816: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
  result = getattr(x, name)(y)

TypeErrorTraceback (most recent call last)
<ipython-input-32-f6bcd943c26a> in <module>()
      3 for brand in common_brands:
----> 4     brand_only = autos[autos["power_ps"] == brand]
      5     mean_ps = autos["power_ps"].mean()
      6     brand_power_ps_mean[brand]

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/ops.py in wrapper(self, other, axis)
    878             with np.errstate(all='ignore'):
--> 879                 res = na_op(values, other)
    880             if is_scalar(res):
    881                 raise TypeError('Could not compare {typ} type with Series'

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/ops.py in na_op(x, y)
    816                     result = getattr(x, name)(y)
    817                 if result is NotImplemented:
--> 818                     raise TypeError("invalid type comparison")
    819             except AttributeError:
    820                 result = op(x, y)

TypeError: invalid type comparison

hey @ANDI

Please share your jupyter notebook for us to help you with this issue. you may refer to this post for submitting your notebook here.

Sharing Your Guided Project in the Community

Basics-Copy1.ipynb (135.8 KB)

Thank you

Click here to view the jupyter notebook file in a new tab

hey @andi

took me a while to understand what was going wrong :grimacing:

  • you are comparing the power_ps column against brand values which is like - comparing if 158 == Volkswagen which will not work.
    Also according to this StackOverflow post, this is not related to pandas but actually to NumPy.

  • the other thing I noticed is you altered dictionary here. Although the code since hasn’t reached there wouldn’t give you that issue yet.

Lastly, if you see the code for brand_mean_prices you have already filtered out rows from autos dataframe using brand, and then you are calculating mean price. This works fine because you are comparing brand with brand. (bmw == bmw!) So you don’t need to again do the select and slice. You can use the sliced dataframe only_brand to also calculate the mean of `power_ps.

Aii I am blind :joy: :sweat_smile:

Great Thank you!

1 Like