I'm getting this very strange error when trying to follow the following exercise on using the corr() method in Python
https://www.geeksforgeeks.org/python-pandas-dataframe-corr/
Specifically, when I try to run the following code: df.corr(method ='pearson')
The error message offers no clue. I thought the corr() method was supposed to automatically ignore strings and empty values etc.
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
df.corr(method='pearson')
File "C:\Users\d.o\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\core\frame.py", line 10059, in corr
mat = data.to_numpy(dtype=float, na_value=np.nan, copy=False)
File "C:\Users\d.o\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\core\frame.py", line 1838, in to_numpy
result = self._mgr.as_array(dtype=dtype, copy=copy, na_value=na_value)
File "C:\Users\d.o\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\core\internals\managers.py", line 1732, in as_array
arr = self._interleave(dtype=dtype, na_value=na_value)
File "C:\Users\d.o\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\core\internals\managers.py", line 1794, in _interleave
result[rl.indexer] = arr
ValueError: could not convert string to float: 'Avery Bradley'
since pandas version 2.0.0 now you need to add numeric_only=True
param to avoid the issue
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With