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

huangapple go评论75阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年4月11日 02:41:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75979773.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定