Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Querying mysql multiple columns using list in python

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading