q = f"""
SELECT * FROM table;
"""
df = pd.read_sql(q, con=conn)
I have multiple columns that I store in python list [col1, col2, ... , coln]
Whenever I want to query all columns except a few, query all columns then dropping is method currently being used.
I am wondering if passing in list of columns in q
q = f""" SELECT {column_list} FROM table"""
is possible.
Reason for this is to save memory in jupyter notebook by bringing in only necessary columns to the notebook.
>Solution :
You could create a CSV list of columns from your list of column names:
col_list = ['col1', 'col2', 'col3']
cols = ', '.join(col_list)
q = f"""
SELECT {cols} FROM table;
"""
df = pd.read_sql(q, con=conn)
print(q) # SELECT col1, col2, col3 FROM table;