Challenge: Clean a String Column

Screen Link:

My Code:

laptops["weight"] = laptops["weight"].str.replace("kg","")
laptops["weight"] = laptops["weight"].str.replace("kgs","")
laptops["weight"] = laptops["weight"].astype(float)

laptops.rename({"weight":"weight_kg"}, axis=1, inplace=True)

laptops.to_csv("laptops_cleaned.csv", index=False)

What I expected to happen: I expect no errors. My code seems the same as the answer, just less concise

What actually happened: Error

ValueErrorTraceback (most recent call last)
<ipython-input-1-71f2a0327646> in <module>()
      1 laptops["weight"] = laptops["weight"].str.replace("kg","")
      2 laptops["weight"] = laptops["weight"].str.replace("kgs","")
----> 3 laptops["weight"] = laptops["weight"].astype(float)
      4 
      5 laptops.rename({"weight":"weight_kg"}, axis=1, inplace=True)

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/util/_decorators.py in wrapper(*args, **kwargs)
    116                 else:
    117                     kwargs[new_arg_name] = new_arg_value
--> 118             return func(*args, **kwargs)
    119         return wrapper
    120     return _deprecate_kwarg

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
   4002         # else, only a single dtype is given
   4003         new_data = self._data.astype(dtype=dtype, copy=copy, errors=errors,
-> 4004                                      **kwargs)
   4005         return self._constructor(new_data).__finalize__(self)
   4006 

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/internals.py in astype(self, dtype, **kwargs)
   3460 
   3461     def astype(self, dtype, **kwargs):
-> 3462         return self.apply('astype', dtype=dtype, **kwargs)
   3463 
   3464     def convert(self, **kwargs):

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/internals.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
   3327 
   3328             kwargs['mgr'] = self
-> 3329             applied = getattr(b, f)(**kwargs)
   3330             result_blocks = _extend_blocks(applied, result_blocks)
   3331 

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/internals.py in astype(self, dtype, copy, errors, values, **kwargs)
    542     def astype(self, dtype, copy=False, errors='raise', values=None, **kwargs):
    543         return self._astype(dtype, copy=copy, errors=errors, values=values,
--> 544                             **kwargs)
    545 
    546     def _astype(self, dtype, copy=False, errors='raise', values=None,

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/internals.py in _astype(self, dtype, copy, errors, values, klass, mgr, **kwargs)
    623 
    624                 # _astype_nansafe works fine with 1-d only
--> 625                 values = astype_nansafe(values.ravel(), dtype, copy=True)
    626                 values = values.reshape(self.shape)
    627 

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/dtypes/cast.py in astype_nansafe(arr, dtype, copy)
    701 
    702     if copy:
--> 703         return arr.astype(dtype)
    704     return arr.view(dtype)
    705 

ValueError: could not convert string to float: '4s'

Isn’t my code the same as the code displayed as the answer below? The only difference I can see is that the answer is more concise…

laptops["weight"] = laptops["weight"].str.replace("kgs","").str.replace("kg","").astype(float)
laptops.rename({"weight": "weight_kg"}, axis=1, inplace=True)
laptops.to_csv('laptops_cleaned.csv',index=False)

Hi @davidaguilaratx, welcome to the community!

Actually, there is a key difference between your code and the solution, and it doesn’t have to do with being concise. The very last line of the error message gives a clue to what went wrong, so look carefully at what’s different besides the length of code.

If you’re still stuck, have a look at this post and it should clear things up.

Happy coding!

Thanks for the welcome and for clearing that up @april.g

It all makes sense now, and when things make sense, I’m happy :slight_smile:

Cheers!