I received this error when trying to compile my code. I extracted data from xlsx file and created a dataframe ,replaced null values with 0, converted all the values to sting to be able to scatterplot and when i tried to show the results of my linear regression I received this error.
TypeError: unsupported operand type(s) for /: 'str' and 'int'
and this is the code I did so far
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
def predict(x):
return slope * x + intercept
from scipy import stats
xlsxfile = pd.ExcelFile("C:\\Users\\AchourAh\\Desktop\\PL14_IPC_03_09_2018_SP_Level.xlsx")
data = xlsxfile.parse('Sheet1', index_col = None, header = None)
data1 = data.fillna(0) #Replace null values of the whole dataset with 0
data1 = data1.astype(str)
print(data1)
X = data1.iloc[0:len(data1),1]
print(X)
Y = data1.iloc[0:len(data1),2]
print(Y)
axes = plt.axes()
axes.grid()
plt.scatter(X,Y)
slope, intercept, r_value, p_value, std_err = stats.linregress(X, Y)
To notice that I am a beginner with this. The last line is causing the error This is the first columns COP COR and PAUS of the dataframe which I am trying to apply some Linear regression on:
0 PP SP000045856 COP COR SP000045856 PAUS
1 201723 0 2000
2 201724 12560 40060
3 201725 -17760 15040
4 201726 -5840 16960
5 201727 10600 4480
6 201728 0 14700
7 201729 4760 46820
... till line 27