【问题标题】:read postgres array from sql table using python pandas使用 python pandas 从 sql 表中读取 postgres 数组
【发布时间】:2017-07-20 19:25:59
【问题描述】:

我尝试从 pandas 中读取 postgres 数组,如下所示:

import pandas as pd
import psycopg2 as pg
con = pg.connect(con_info)
df = pd.read_sql(con,'select my_arr from my_table')

但是我得到的是这样的字符串:

 my_arr
 ---------
'{1,2,3}'

我可以解析字符串,但我想在没有任何字符串操作的情况下将它作为 python 列表。

谢谢。

约瑟夫。

【问题讨论】:

    标签: python postgresql pandas


    【解决方案1】:

    找到了一个答案,使用 array_to_json 解决了这个问题。

    Convert PostgreSQL array to PHP array 在python中:

    df = pd.read_sql(con,'select array_to_json(my_arr) from my_table')
    

    列中的数据类型是 python 列表。 但是,我想知道是否有更自然的方法来做到这一点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-26
      • 2021-10-07
      • 1970-01-01
      • 1970-01-01
      • 2019-04-24
      • 1970-01-01
      相关资源
      最近更新 更多