I am using python and PSQL to extract data but what I would want to do is to retrieve data from a specific table using a list that contains the name of desired columns.
*args
could have any number of names inside it so I was wondering how to do it.
Function to read table with a given query:
def read_table_data(table_name, query, conn):
table_name = pd.read_sql_query(query, conn)
return table_name
Function to extract the data:
def extract_variable_table_many_columns(table, *args):
df = pd.DataFrame()
col = []
for c in args:
print(c)
col.append(c)
query_extract_variables = "SELECT {} FROM {};".format(str(col),str(table))
df1 = read_table_data("{}".format(str(table)), query_extract_variables , conn)
df = df.append(df1)
return df