0

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.

enter image description here

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'

UPDATED: enter image description here

enter image description here

Sajid Latif
  • 119
  • 2
  • 16
  • 3
    the first two rows of your dataframe contains string values in Total column. – Rajat Mishra Dec 27 '20 at 13:42
  • Look at this : https://stackoverflow.com/questions/52153414/how-to-pre-process-data-before-pandas-read-csv – Mitzi Dec 27 '20 at 13:45
  • Does this answer your question? [Convert pandas.Series from dtype object to float, and errors to nans](https://stackoverflow.com/questions/25952790/convert-pandas-series-from-dtype-object-to-float-and-errors-to-nans) – Håken Lid Dec 27 '20 at 13:47
  • @HåkenLid I assume the number 0,75 can not be a number as long as it doesn't have a dot inside. So instead of 0,75 it should be 0.75 (with a "." instead of ",") – Sajid Latif Dec 27 '20 at 13:53
  • @RajatMishra I have dropped the two first column but I still have the issue. I assume it is the "," instead of "." in the decimal number – Sajid Latif Dec 27 '20 at 13:55
  • The error message tells you exactly what is wrong. The string "Total" can't be parsed as a number. What number do you expect to get? – Håken Lid Dec 27 '20 at 14:04
  • @HåkenLid: your right. I have now removed the first two lines, so the value "Total" is not a part of the col value. But I now get this error "AttributeError: 'Series' object has no attribute 'Total'" See the updated image – Sajid Latif Dec 27 '20 at 14:07
  • 1
    Series and DataFrame are different types. There's lots of learning resources about pandas here: https://pandas.pydata.org/pandas-docs/stable/getting_started/tutorials.html – Håken Lid Dec 27 '20 at 14:13
  • Okay, great. Lot of video materials. I will look it through :) – Sajid Latif Dec 27 '20 at 14:15

1 Answers1

2

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.

yash
  • 122
  • 8