Reputation: 119
I can not change my string to float value. Im new at Python and could not find any similar issues on stack overflow. Sorry if I have missed it...
Snippet of my data.
My code looks like this
import pandas as pd
import numpy as np
data = pd.read_csv('data.csv')
dt = data.rename(columns = {"Unnamed: 0": "test", "Unnamed: 1": "name", "Unnamed: 2": "week"})
dt.dtypes
Output
test object
name object
week object
Total object
§83 object
§83.1 object
Unnamed: 6 object
Unnamed: 7 object
Unnamed: 8 object
Unnamed: 9 object
Unnamed: 10 object
§83.2 object
Unnamed: 12 object
Unnamed: 13 object
Unnamed: 14 object
Unnamed: 15 object
Unnamed: 16 object
§83a object
Unnamed: 18 object
Unnamed: 19 object
Unnamed: 20 object
Unnamed: 21 object
Unnamed: 22 object
dtype: object
Want to change my string in Col Total to float with this
dt.Total = dt.Total.astype(float)
Error: ValueError: could not convert string to float: 'Total'
See below for total info
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-78-ad6ed6a5e004> in <module>
----> 1 dt.Total = dt.Total.astype(float)
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors)
5546 else:
5547 # else, only a single dtype is given
-> 5548 new_data = self._mgr.astype(dtype=dtype, copy=copy, errors=errors,)
5549 return self._constructor(new_data).__finalize__(self, method="astype")
5550
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/internals/managers.py in astype(self, dtype, copy, errors)
602 self, dtype, copy: bool = False, errors: str = "raise"
603 ) -> "BlockManager":
--> 604 return self.apply("astype", dtype=dtype, copy=copy, errors=errors)
605
606 def convert(
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/internals/managers.py in apply(self, f, align_keys, **kwargs)
407 applied = b.apply(f, **kwargs)
408 else:
--> 409 applied = getattr(b, f)(**kwargs)
410 result_blocks = _extend_blocks(applied, result_blocks)
411
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/internals/blocks.py in astype(self, dtype, copy, errors)
593 vals1d = values.ravel()
594 try:
--> 595 values = astype_nansafe(vals1d, dtype, copy=True)
596 except (ValueError, TypeError):
597 # e.g. astype_nansafe can fail on object-dtype of strings
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/dtypes/cast.py in astype_nansafe(arr, dtype, copy, skipna)
995 if copy or is_object_dtype(arr) or is_object_dtype(dtype):
996 # Explicit copy, or required since NumPy can't view from / to object.
--> 997 return arr.astype(dtype, copy=True)
998
999 return arr.view(dtype)
ValueError: could not convert string to float: 'Total'
Upvotes: 0
Views: 3617
Reputation: 132
If any of your columns contain a string value (even an alphabet for that matter), python will show an error while trying to convert it to float or int. The first two rows of the TOTAL column are strings. For this reason, you might want to remove these or replace them with some other suitable values before attempting to convert the column values to float.
Upvotes: 2