'Keep seeing TypeError: 'value' must be an instance of str or bytes, not a float
I am relatively new to using Python, I am trying to run a regression with a merged dataset and it keeps showing me the error "TypeError: 'value' must be an instance of str or bytes, not a float". I do not know what it means or how to fix it. Anything Helps!!!
Here is my code
from statsmodels.formula.api import ols
fit = ols('Congregations ~ ViolentCrime ', data=data).fit()
print(fit.summary())
print(fit.params)
import matplotlib.pyplot as plt
plt.plot(data['ViolentCrime'], data['Congregations'], 'ro')
plt.plot(data['ViolentCrime'], fit.params.focal_year*data['ViolentCrime'] + fit.params.Intercept)
plt.ylabel('Congregations')
plt.xlabel('ViolentCrime')
Solution 1:[1]
plt.plot considers its first two positional arguments to be a labels of x and y axes.
Plotting labelled data
There's a convenient way for plotting objects with labelled data (i.e. data that can be accessed by index obj['y']). Instead of giving the data in x and y, you can provide the object in the data parameter and just give the labels for x and y:
>>> plot('xlabel', 'ylabel', data=obj)
All indexable objects are supported. > This could e.g. be a dict, a pandas.DataFrame or a structured numpy array.
Solution 2:[2]
I found the source of the error. Even after removing all commas and periods on my excel sheet, excel was automatically adding commas in the thousands. I found out how to remove them and it worked. I Ctl+A selected all the cells, then right clicked, selected Format Cells, go under the tab 'Number', and unselected the box 'Use 1000 separator (,)'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | madbird |
| Solution 2 |
