-2
In the R I can have this structure
var1 <-TRUE
var2 <-FALSE
paste0("ANO_544, SEMANA",
if(var1 == TRUE){", COD_NEGOCIO"},
if(var2 == TRUE){", CATEGORIA"},
", MODULADO")
And I got an answer:
"ANO_544, SEMANA, COD_NEGOCIO, MODULADO"
This is useful for making SQL requests, such as:
library(DBI)
var1 <-TRUE
var2 <-FALSE
con <- dbConnect(RSQLite::SQLite(), ":memory:")
dbWriteTable(con, "mtcars", mtcars)
var1 <-FALSE
var2 <-TRUE
dbGetQuery(con, paste0("SELECT
disp,",
if(var1 == TRUE){"gear,"},
if(var2 == TRUE){"carb,"},
"avg(qsec)
FROM mtcars
group by
disp",
if(var1 == TRUE){",gear"},
if(var2 == TRUE){",carb"}))
dbDisconnect(con)
So I can change the query by changing the values of var1
and var1
.
The question: Is there a structure like this in Python?
I can do it inside the pd.read_sql(" ".join([...]), cursor)
?
Explain your problem better, I don’t understand what you want to do and what kind of logical operation to run over the array. Where’s your Python code? Put it to make it easy.
– gato
It is not very clear what you want to do. Do you want the return to be an array? And based on conditionals it contains or not certain elements?
– Jorge Nachtigall