【问题标题】:Pandas how to append data in the same DataFrame using a for loop? [duplicate]Pandas 如何使用 for 循环在同一个 DataFrame 中附加数据? [复制]
【发布时间】:2019-05-31 00:26:35
【问题描述】:

目前我的代码如下所示:

conn = psycopg2.connect("dbname=monty user=postgres host=localhost password=postgres")
cur = conn.cursor()
cur.execute("SELECT * FROM binance.zrxeth_ob_indicators;")
for row in cur:
  df = pd.DataFrame(row,columns=['timestamp', 'topAsk', 'topBid', 'CPA', 'midprice', 'CPB', 'spread', 'CPA%', 'CPB%'])

但是数据并没有添加到数据框中,它只是创建一个新的?关于如何附加到现有的相同 DataFrame 的任何想法?谢谢!

【问题讨论】:

  • 你的意思是for row in cur.fetchall()

标签: python pandas


【解决方案1】:

你试过了吗

df=pd.read_sql_query(“””select * from table “””, conn)

它将所有查询结果直接读入数据框

【讨论】:

    猜你喜欢
    • 2019-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-12
    • 2016-11-22
    • 2022-01-04
    相关资源
    最近更新 更多