从字符串中移除双引号-当为函数输入值,以便只需写入一次

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

Remove double quotes from a string- when inputting a value for a function so it can be written just once

问题

So, I have the following code:

corrresult = df1.drop("COMPRIMISED_ALERT", axis=1).apply(lambda x: x.corr(df1.COMPRIMISED_ALERT))

我有以下代码:

corrresult = df1.drop("COMPRIMISED_ALERT", axis=1).apply(lambda x: x.corr(df1.COMPRIMISED_ALERT))

I want to turn this into a function, so I can use different variables for both the dataframe and the column I'm dropping/ using for the correlation vector.

我想将这转化为一个函数,这样我可以使用不同的变量来表示数据框和用于相关向量的列。

The code below won't work, as y needs to be entered once with quotes marks and once without.

以下代码不起作用,因为 y 需要在带引号和不带引号的情况下分别输入。

def correlation_vector(d, v): corrresult = d.drop(v, axis=1).apply(lambda x: x.corr(d.v)) return corrresult

Any idea how to fix this without needing to add a third variable as follows (the third would literally be the second without speech marks):

有没有办法修复这个问题,而不需要像下面这样添加第三个变量(第三个变量实际上就是第二个去掉引号的版本):

def correlation_vector(d, v, v2): corrresult = d.drop(v, axis=1).apply(lambda x: x.corr(d.v2)) return corrresult

If applied to the initial code:
如果应用于初始代码:
d = df1 v = "COMPRIMISED_ALERT" v2 = COMPRIMISED_ALERT

英文:

So, I have the following code:

corrresult = df1.drop("COMPRIMISED_ALERT", axis=1).apply(lambda x: x.corr(df1.COMPRIMISED_ALERT))

I want to turn this into a function, so I can use different variables for both the dataframe and the column I'm dropping/ using for the correlation vector

The code below won't work, as y needs to be entered once with quotes marks and once without.

def correlation_vector (d,v):
corrresult = d.drop(v, axis=1).apply(lambda x: x.corr(d.v))
return corrresult

Any idea how to fix this without needing to add a third variable as follows (the third would literally be the second without speech marks):

def correlation_vector (d,v,v2):
corrresult = d.drop(v, axis=1).apply(lambda x: x.corr(d.v2))
return corrresult

If applied to the initial code:
d = df1
v = "COMPRIMISED_ALERT"
v2 = COMPRIMISED_ALERT

答案1

得分: 0

尽管 DataFrame 中的列可以通过 类似字典 的方式 ["col_name"] 或使用 点属性符号 .col_name(其中列名是有效的 Python 变量名)来检索,但我建议您使用前者。

def correlation_vector(d, v):
    corrresult = d.drop(v, axis=1).apply(lambda x: x.corr(d[v])) # <- d[v] 而不是 d.v 
    return corrresult

要调用您的函数,请使用:

correlation_vector(df1, "COMPRIMISED_ALERT")
英文:

Although the columns in a DataFrame can be retrieved either by dictionary-like notation ["col_name"] or by using the dot attribute notation .col_name (where the column name is a valid Python variable name), I suggest you to use the former.

def correlation_vector(d, v):
    corrresult = d.drop(v, axis=1).apply(lambda x: x.corr(d[v])) # <- d[v] instead of d.v 
    return corrresult

To call your function, use :

correlation_vector(df1, "COMPRIMISED_ALERT")

huangapple
  • 本文由 发表于 2023年4月10日 19:52:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75976853.html
匿名

发表评论

匿名网友

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

确定