ValueError Traceback (most recent call last)ġ7 print ('Average of all ages is \t', an(). Print ("Maximum age is %d with %s pid, whose BMI is %s"\ Print ('SD of all ages is \t', adata.std().round(2)) Print ('Minimum of all ages is \t', np.min(adata).round(2)) Typically, this occurs when the string object has an incorrect floating number with spaces. If you want to catch that line, try this code. Your test code works because the word id isn't present in line 2. Try to output your variables and their types to makes you see where you are trying to convert the float. I have no idea why this is happening, 'numbercol' is already a float column. Like the comments said, you are trying to convert a string or object (in this case 'Male') into a float. > import seaborn as sns > sns.distplot (df, x'numbercol') But I got this following error: ValueError: could not convert string to float: 'numbercol'. Somewhere in your text file, a line has the word id in it, which can't really be converted to a number. And I want to plot histogram using seaborn and column numbercol. Print ('Maximum of all ages is \t', adata.max().round(2)) Only particular string values can be converted to floats in Python. This error is pretty verbose: ValueError: could not convert string to float: id. Print ('Median of all ages is \t', np.median(adata).round(2)) Print ('Average of all ages is \t', an().round(2)) Sheet = xlrd.open_workbook(wb).sheet_by_index(0) You need to specify the columns you want to use and the rows you want to skip: similarityMatrix np.loadtxt ('blosum50.txt', skiprows1, delimiter'\t', usecolsrange (1,numcols+1)) This worked for me with your previous posted code. after running these lines of code I get an error! ValueError: could not convert string to float: '' wb = r"C:\\Users.\df.xls" The problem is that you are reading the non-numeric values from the file. ValueError: could not convert string to float: '9.8-10.I am new to numpy and have a problem. Values = astype_nansafe(values, dtype, copy=copy)įile "C:\Users\shweta\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\dtypes\cast.py", line 1181, in astype_nansafe New_values = astype_array(values, dtype, copy=copy)įile "C:\Users\shweta\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\dtypes\cast.py", line 1237, in astype_array New_values = astype_array_safe(values, dtype, copy=copy, errors=errors)įile "C:\Users\shweta\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\dtypes\cast.py", line 1292, in astype_array_safe Naive Bayes Gaussian throwing ValueError: could not convert string to float: 'M' 1. The problem is that your string is not just 1151226468812.22, but it is '1151226468812.22'. Return self.apply("astype", dtype=dtype, copy=copy, errors=errors)įile "C:\Users\shweta\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\internals\managers.py", line 304, in applyįile "C:\Users\shweta\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\internals\blocks.py", line 580, in astype ValueError: could not convert string to float - machine learning. New_data = self._mgr.astype(dtype=dtype, copy=copy, errors=errors)įile "C:\Users\shweta\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\internals\managers.py", line 419, in astype The first will only set the value if the key doesnt exist in the dict at all, so you would still get an empty string. import osĭf = df.str.replace(r'.* (+)+$', r'\1',regex=True).str.replace('km/litre', '',regex=True).astype('float')ĮRROR:- Traceback (most recent call last):įile "c:\Users\shweta\OneDrive\Desktop\DEMO\demo\src\carsengage2022\preprocessing\roughKMC.py", line 9, in įile "C:\Users\shweta\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\generic.py", line 5912, in astype So I'm thinking of removing 10.0 and converting 9.8 to float, but I'm not getting how to do it. I'm trying to convert the values of ARAI_Certified_Mileage column to float but it is throwing an error that string values like '9.8-10.0' can't be converted to float.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |