Python:列引用之间的函数参数
基本上,我需要以下几点:Python:列引用之间的函数参数,python,function,Python,Function,基本上,我需要以下几点: data['sales']*10 data['height']*10 我面临的根本问题是如何创建一个函数,在这个函数中,我可以不使用 在函数中添加引号。 这可能吗?例如,在“”中写入一个特殊字符,表示单词是参数 def function(var1): p=data['var1']*10 #The error is here; I tried p=data["'"+var1+"'"]*10 #Is there a way
data['sales']*10
data['height']*10
我面临的根本问题是如何创建一个函数,在这个函数中,我可以不使用
在函数中添加引号。
这可能吗?例如,在“”中写入一个特殊字符,表示单词是参数
def function(var1):
p=data['var1']*10 #The error is here; I tried p=data["'"+var1+"'"]*10
#Is there a way to indicate var1 is not a string,
#like p=data['&var1']*10
return p
function(sales)
function(height)
我知道这个问题很基本,但我需要知道这是否可能。如果没有,我将创建所有函数并为每个参数添加引号。谢谢。您只需将字符串(字段/列名)作为参数传递给函数,该参数将存储在变量
var1
中。这样,就不需要在函数中的var1
周围加引号了。例如,请执行以下操作
def function(var1):
p=data[var1]*10 #The error is here; I tried p=data["'"+var1+"'"]*10
#Is there a way to indicate var1 is not a string,
#like p=data['&var1']*10
return p
function('sales')
function('height')
只需将字符串(字段/列名)作为参数传递给函数,该参数将存储在变量
var1
中。这样,就不需要在函数中的var1
周围加引号了。例如,请执行以下操作
def function(var1):
p=data[var1]*10 #The error is here; I tried p=data["'"+var1+"'"]*10
#Is there a way to indicate var1 is not a string,
#like p=data['&var1']*10
return p
function('sales')
function('height')