英文:
Pandas: Why are the values returned in one instance and just the df name in another? In the latter case I have the run another line of code
问题
以下是翻译好的代码部分:
如果我将值输入以下函数,我会得到输出值:
#相关向量
#排除目标变量本身,找到前n个按绝对值降序排列的特征
def correlation_vector(d, v, n):
corrresult = d.drop(v, axis=1).apply(lambda x: x.corr(d[v])) # <- d[v]
return corrresult
然而,当我在函数中添加 'dropna(inplace=True)'(第4行)时,输出是生成的数据框的名称?我必须在之后运行数据框本身才能获取值:
#相关向量
#排除目标变量本身,找到前n个按绝对值降序排列的特征
def correlation_vector(d, v, n):
corrresult = d.drop(v, axis=1).apply(lambda x: x.corr(d[v])).dropna(inplace=True)
return corrresult
英文:
If I input values into the function below, I get output values:
#Correlation vector
#Excluding the target variable itself, find the top n features- ranked by absolute value desc
def correlation_vector(d, v, n):
corrresult = d.drop(v, axis=1).apply(lambda x: x.corr(d[v])) # <- d[v]
return corrresult
However, when I add 'dropna(inplace=True)' to the function (4th line), the output is the name of the resulting df? I have to run the df itself after to get the values:
#Correlation vector
#Excluding the target variable itself, find the top n features- ranked by absolute value desc
def correlation_vector(d, v, n):
corrresult = d.drop(v, axis=1).apply(lambda x: x.corr(d[v])).dropna(inplace=True)
return corrresult
答案1
得分: 1
如果您将函数修改为以下方式,会怎么样?
#相关性向量
def correlation_vector(d, v, n):
corrresult = d.drop(v, axis=1).apply(lambda x: x.corr(d[v]))
corrresult.dropna(inplace=True)
return corrresult
英文:
What about if you modify your function in something like this?
#Correlation vector
def correlation_vector(d, v, n):
corrresult = d.drop(v, axis=1).apply(lambda x: x.corr(d[v]))
corrresult.dropna(inplace=True)
return corrresult
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论