Python 如何从数据库中的select创建数据集?

Python 如何从数据库中的select创建数据集?,python,Python,我有一个数据库postgresql,我在表iaconsultas中进行选择 conn = psycopg2.connect(host="10.1...", port="5432", database="dbname", user="username", password="pwd") cur = conn.cursor() cur.execute("SELECT dsobservacao,cid FROM iaconsultas limit 100") col_names = [i[0] for

我有一个数据库postgresql,我在表
iaconsultas
中进行选择

conn = psycopg2.connect(host="10.1...", port="5432", database="dbname", user="username", password="pwd")
cur = conn.cursor()
cur.execute("SELECT dsobservacao,cid FROM iaconsultas limit 100")
col_names = [i[0] for i in cur.description]
# retrieve the records from the database
records = cur.fetchall()

#print(col_names) print columns name
#print(records) print data
变量col_names向我显示colunase记录的名称,以及每个记录中输入的值。 我正在学习机器学习,希望为标签创建一个包含列值及其各自名称的数据集

数据集,例如:

dsoberservacao;cid
valueinserted;idcid

有可能吗?我怎么做?谢谢大家!

可能想看看熊猫及其方法。在您的例子中,它类似于
df=pd.read_sql(“从iaconsultas LIMIT 100,conn中选择DSobservaco,cid)
这会创建一个数据框,标签是列的名称及其值吗?类似于df.columns=['text','cid']是的,使用您的查询,列名将是
dsobservaco
cid
。这些值将是查询获取的前100条记录的列的值。