我有一个函数,它绘制了Pandas
DataFrame
中两列的日志。因为这样的零会导致错误并需要删除。此时该函数的输入是DataFrame
的两列。有没有办法删除任何包含零的行?例如DF = DF的等效版本[df.ColA!= 0]删除两个Pandas系列中包含零的整行
def logscatfit(x,y,title):
xvals2 = np.arange(-2,6,1)
a = np.log(x) #These are what I want to remove the zeros from
b = np.log(y)
plt.scatter(a, b, c='g', marker='x', s=35)
slope, intercept, r_value, p_value, std_err = stats.linregress(a,b)
plt.plot(xvals2, (xvals2*slope + intercept), color='red')
plt.title(title)
plt.show()
print "Slope is:",slope, ". Intercept is:",intercept,". R-value is:",r_value,". P-value is:",p_value,". Std_err is:",std_err
在想不到的两个a
和b
去除零,但让他们同样长度,使得的方式,我可以绘制散点图。是我唯一的选择重写函数采取DataFrame
,然后删除零如df1 = df[df.ColA != 0]
然后df2 = df1[df1.ColB != 0]
?