【问题标题】:Can't return Array from database无法从数据库返回数组
【发布时间】:2020-04-17 13:35:44
【问题描述】:

我的 Snowflake 数据库中有一个表。我想从列中获取数据。

数据示例:

id          tid           categories
------------------------------------------------------------------------------
v-123       t-123         ["tx-b5mmqcpooa0p","tx-b5mmqcpood0p"]
b-123       t-124         ["tx-b5mmfcpooa0p","tx-b4mmqcpood0p"]
x-123       t-125         []
b-123       t-124         ["tx-b5mmfcpooa0p","tx-b4mmqcpood0p","tx-bmqcpood0p"]

我尝试使用熊猫:

sql = SELECT id, tid, categories FROM table 
df = pandas.read.sql(con, sql)

我得到了这个结果:

id          tid           categories
---------------------------------------------------------------------------
"v-123"       "t-123"         " ["tx-b5mmqcpooa0p","tx-b5mmqcpood0p"] "
"b-123"       "t-124"         " ["tx-b5mmfcpooa0p","tx-b4mmqcpood0p"] "
"x-123"       "t-125"         " [] "
"b-123"       "t-124"         " ["tx-b5mmfcpooa0p","tx-b4mmqcpood0p","tx-bmqcpood0p"] "

“类别”的类型是字符串而不是数组(列表)。

我做错了什么?

谢谢!

【问题讨论】:

    标签: python sql dataframe snowflake-cloud-data-platform


    【解决方案1】:

    【讨论】:

    • 是的,你是对的df['categories'] = df['categories'].apply(lambda x: json.loads(x) if x else [])
    猜你喜欢
    • 1970-01-01
    • 2015-04-06
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-27
    • 2019-01-26
    • 1970-01-01
    相关资源
    最近更新 更多