在 python 中将查询结果转换为 DataFrame
Converting query results into DataFrame in python
我正在尝试对使用 psycog2 的查询结果进行操作。因此我必须将结果转换为 pandas DataFrame。但是当我使用下面的代码并打印时,只打印列名而不是行。我也用了 'pd.DataFrame.from_records',但没用。
import psycopg2
import pandas as pd
import numpy as np
conn_string = "Connect_Info"
conn = psycopg2.connect(conn_string)
cursor = conn.cursor()
cursor.execute(query)
rows=pd.DataFrame(cursor.fetchall(),columns=['page_num','Frequency'])
for row in rows:
print row
conn.commit();
conn.close();
cursor.fetchall() 的结果 -
(1L, 90990L)
(3L, 6532L)
(2L, 5614L)
(4L, 4016L)
(5L, 2098L)
(6L, 1651L)
(7L, 1158L)
(8L, 854L)
(9L, 658L)
(10L, 494L)
(11L, 345L)
(12L, 301L)
(13L, 221L)
(15L, 152L)
(14L, 138L)
(16L, 113L)
(17L, 93L)
(18L, 73L)
(20L, 62L)
(19L, 55L)
(22L, 44L)
(21L, 35L)
(23L, 29L)
(25L, 24L)
(27L, 19L)
(26L, 18L)
这正是您在数据框上迭代时应该发生的情况,您会看到列名。如果您想查看 df,只需打印 df。要查看行:
for ind, row in df.iterrows():
print(row.values)
或.values:
for row in df.values:
print(row)
另一个建议是使用 itertuples,它产生 (index, row_value1, row_value2...) 元组。
for tup in rows.itertuples():
print tup
'(0, 1, 90990)
(1, 3, 6532)
(2, 2, 5614)
(3, 4, 4016)
...'
可以看到第一个位置是索引,socend是第一列的值,第二个是第二列的值。
也许不能直接回答您的问题,但您应该使用 read_sql_query
而不是自己执行 fetchall 并包装在 DataFrame 中。这看起来像:
conn = psycopg2.connect(...)
rows = pd.read_sql_query(query, conn)
而不是上面的所有代码。
对于您的实际问题,请参阅 http://pandas.pydata.org/pandas-docs/stable/basics.html#iteration 以了解解释和不同的选项。
基础知识是遍历数据框,遍历列名。要遍历行,您可以使用其他函数,例如 .iterrows()
和 .itertuples()
。但请记住,在大多数情况下,不需要手动遍历行。
我正在尝试对使用 psycog2 的查询结果进行操作。因此我必须将结果转换为 pandas DataFrame。但是当我使用下面的代码并打印时,只打印列名而不是行。我也用了 'pd.DataFrame.from_records',但没用。
import psycopg2
import pandas as pd
import numpy as np
conn_string = "Connect_Info"
conn = psycopg2.connect(conn_string)
cursor = conn.cursor()
cursor.execute(query)
rows=pd.DataFrame(cursor.fetchall(),columns=['page_num','Frequency'])
for row in rows:
print row
conn.commit();
conn.close();
cursor.fetchall() 的结果 -
(1L, 90990L)
(3L, 6532L)
(2L, 5614L)
(4L, 4016L)
(5L, 2098L)
(6L, 1651L)
(7L, 1158L)
(8L, 854L)
(9L, 658L)
(10L, 494L)
(11L, 345L)
(12L, 301L)
(13L, 221L)
(15L, 152L)
(14L, 138L)
(16L, 113L)
(17L, 93L)
(18L, 73L)
(20L, 62L)
(19L, 55L)
(22L, 44L)
(21L, 35L)
(23L, 29L)
(25L, 24L)
(27L, 19L)
(26L, 18L)
这正是您在数据框上迭代时应该发生的情况,您会看到列名。如果您想查看 df,只需打印 df。要查看行:
for ind, row in df.iterrows():
print(row.values)
或.values:
for row in df.values:
print(row)
另一个建议是使用 itertuples,它产生 (index, row_value1, row_value2...) 元组。
for tup in rows.itertuples():
print tup
'(0, 1, 90990)
(1, 3, 6532)
(2, 2, 5614)
(3, 4, 4016)
...'
可以看到第一个位置是索引,socend是第一列的值,第二个是第二列的值。
也许不能直接回答您的问题,但您应该使用 read_sql_query
而不是自己执行 fetchall 并包装在 DataFrame 中。这看起来像:
conn = psycopg2.connect(...)
rows = pd.read_sql_query(query, conn)
而不是上面的所有代码。
对于您的实际问题,请参阅 http://pandas.pydata.org/pandas-docs/stable/basics.html#iteration 以了解解释和不同的选项。
基础知识是遍历数据框,遍历列名。要遍历行,您可以使用其他函数,例如 .iterrows()
和 .itertuples()
。但请记住,在大多数情况下,不需要手动遍历行。