简体   繁体   English

在 postgres 中将列数据类型从 VARCHAR 转换为 ARRAY(使用 Psycopg2)

[英]Converting column datatype from VARCHAR to ARRAY in postgres (using Psycopg2)

I have a pandas dataframe df, which I want to store as a schema in a database.我有一个 Pandas 数据帧 df,我想将其作为模式存储在数据库中。 By default, it is taking text as the default datatype.默认情况下,它将文本作为默认数据类型。 I used dtype=sqlalchemy.types.VARCHAR argument to save the columns as VARCHAR.我使用 dtype=sqlalchemy.types.VARCHAR 参数将列保存为 VARCHAR。

df.to_sql('data', con, schema='schema', index= False, dtype=sqlalchemy.types.VARCHAR)

However, the datatype of one of my column is a multidimensional "array".但是,我的一列的数据类型是多维“数组”。 I tried following code(s), but I am not getting expected output.我尝试了以下代码,但没有得到预期的输出。

cursor.execute("
alter table data alter col2 drop default;
alter table data alter col2 type text[][] using array[col2];
alter table data alter col2 set default '{}'");

The above code is converting the desired column to text array but the array is empty.上面的代码正在将所需的列转换为文本数组,但该数组为空。

My data looks like this:我的数据如下所示:

col1     col2
A1       A1:DEF, Human; X2:XYZ, Mouse;  Y1:RST, Rat
B1       B1:GHI, Human; Y2:ZXY, Mouse
C1       C1:JKL, Human; Z2:USC, Mouse

I would like to store col1 as VARCHAR and col2 as multidimensional array of size n, storing each part separated by ';'我想将 col1 存储为 VARCHAR,将 col2 存储为大小为 n 的多维数组,存储每个部分由 ';' 分隔as one element.作为一个元素。

For A1:
array[1]: A1:DEF, Human
array[2]: X2:XYZ, Mouse
array[3]: Y1:RST, Rat

Any suggestion, how to make it work?任何建议,如何使它工作?

Thanks谢谢

Use the function string_to_array() :使用函数string_to_array()

alter table data alter col2 drop default;
alter table data alter col2 type text[] using string_to_array(col2, '; ');
alter table data alter col2 set default '{}';

Next update the table if you really want to get multidimensional arrays:如果你真的想得到多维数组,接下来更新表:

update data d
set col2 = s.col2
from (
    select col1, array_agg(string_to_array(elem, ', ')) as col2
    from data
    cross join unnest(col2) elem
    group by col1) s
where d.col1 = s.col1;

Db<>Fiddle. Db<>小提琴。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM