Basically I need the following:
data['sales']*10
data['height']*10
The underlying problem I am facing is how can I create a function where I can write the variable names without adding the quotations inside the function. Is this possible? For example, like writing a special character inside the " " that indicates a word is an argument.
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)
I know the question is very basic, but I need to know if it is possible. If not, i will just create all functions and add the quotations for each argument. Thanks.
You just need to pass the strings (field/column name) to your function as an argument which will be stored into the variable var1
. You then don't need to put the quotes around var1
inside your function. For example, do the following
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')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.