Python 2.7 删除两个系列中包含零的整行
我有一个函数,它可以从Python 2.7 删除两个系列中包含零的整行,python-2.7,pandas,Python 2.7,Pandas,我有一个函数,它可以从PandasDataFrame绘制两列的日志。因此,零会导致错误,需要删除。此时,函数的输入是来自数据帧的两列。有没有办法删除任何包含零的行?例如,df=df[df.ColA!=0]的等效版本 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)
Pandas
DataFrame
绘制两列的日志。因此,零会导致错误,需要删除。此时,函数的输入是来自数据帧的两列。有没有办法删除任何包含零的行?例如,df=df[df.ColA!=0]的等效版本
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
At想不出一种方法来删除a
和b
中的零,但是保持它们的长度相同,这样我就可以绘制散点图了。我唯一的选择是重写函数以获取一个数据帧,然后用类似df1=df[df.ColA!=0]
的东西删除零,然后df2=df1[df1.ColB!=0]
?正如我理解你的问题,你需要删除或者x
或者y
为零的行
一个简单的方法是
keepThese = (x > 0) & (y > 0)
a = x[keepThese]
b = y[keepThese]
然后继续您的代码。我理解您的问题,您需要删除(和/或)x
或y
为零的行
一个简单的方法是
keepThese = (x > 0) & (y > 0)
a = x[keepThese]
b = y[keepThese]
然后继续您的代码。我理解您的问题,您需要删除(和/或)x
或y
为零的行
一个简单的方法是
keepThese = (x > 0) & (y > 0)
a = x[keepThese]
b = y[keepThese]
然后继续您的代码。我理解您的问题,您需要删除(和/或)x
或y
为零的行
一个简单的方法是
keepThese = (x > 0) & (y > 0)
a = x[keepThese]
b = y[keepThese]
然后继续编写代码。将FooBar
的答案插入到函数中会给出:
def logscatfit(x,y,title):
xvals2 = np.arange(-2,6,1)
keepThese = (x > 0) & (y > 0)
a = x[keepThese]
b = y[keepTheese]
a = np.log(a)
b = np.log(b)
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
将FooBar
的答案插入到函数中,可以得到:
def logscatfit(x,y,title):
xvals2 = np.arange(-2,6,1)
keepThese = (x > 0) & (y > 0)
a = x[keepThese]
b = y[keepTheese]
a = np.log(a)
b = np.log(b)
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
将FooBar
的答案插入到函数中,可以得到:
def logscatfit(x,y,title):
xvals2 = np.arange(-2,6,1)
keepThese = (x > 0) & (y > 0)
a = x[keepThese]
b = y[keepTheese]
a = np.log(a)
b = np.log(b)
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
将FooBar
的答案插入到函数中,可以得到:
def logscatfit(x,y,title):
xvals2 = np.arange(-2,6,1)
keepThese = (x > 0) & (y > 0)
a = x[keepThese]
b = y[keepTheese]
a = np.log(a)
b = np.log(b)
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
我喜欢FooBar简单的回答。更通用的方法是将数据帧传递给函数并使用.any()
方法
def logscatfit(df,x_col_name,y_col_name,title):
two_cols = df[[x_col_name,y_col_name]]
mask = two_cols.apply(lambda x: ( x==0 ).any(), axis = 1)
df_to_use = df[mask]
x = df_to_use[x_col_name]
y = df_to_use[y_col_name]
#your code
a = n.log(x)
etc
我喜欢FooBar简单的回答。更通用的方法是将数据帧传递给函数并使用.any()
方法
def logscatfit(df,x_col_name,y_col_name,title):
two_cols = df[[x_col_name,y_col_name]]
mask = two_cols.apply(lambda x: ( x==0 ).any(), axis = 1)
df_to_use = df[mask]
x = df_to_use[x_col_name]
y = df_to_use[y_col_name]
#your code
a = n.log(x)
etc
我喜欢FooBar简单的回答。更通用的方法是将数据帧传递给函数并使用.any()
方法
def logscatfit(df,x_col_name,y_col_name,title):
two_cols = df[[x_col_name,y_col_name]]
mask = two_cols.apply(lambda x: ( x==0 ).any(), axis = 1)
df_to_use = df[mask]
x = df_to_use[x_col_name]
y = df_to_use[y_col_name]
#your code
a = n.log(x)
etc
我喜欢FooBar简单的回答。更通用的方法是将数据帧传递给函数并使用.any()
方法
def logscatfit(df,x_col_name,y_col_name,title):
two_cols = df[[x_col_name,y_col_name]]
mask = two_cols.apply(lambda x: ( x==0 ).any(), axis = 1)
df_to_use = df[mask]
x = df_to_use[x_col_name]
y = df_to_use[y_col_name]
#your code
a = n.log(x)
etc