简体   繁体   中英

How do I select specific columns when using sqlalchemy?

I have code similar to the one below:

# pip install pymysql
# pip install sqlalchemy

import pandas as pd
import sqlalchemy

engine = sqlalchemy.create_engine('mysql+pymysql://username:password@localhost:3306/db_name')

query = '''
select * from table_name
'''
df = pd.read_sql_query(query, engine)
print(df)

I would like to modify the select statement to just list specific columns and not all of them, as above. For example the csv file the code reads has a column name Player. To select the column Player, I tried

query = '''
select Player from table_name
'''

but it does not work. How can I fix this?

This solved it, simple but it did not occur to me. I needed backward slashes with quotes, not just quotes to surround Player.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM