[英]Converting column datatype from VARCHAR to ARRAY in postgres (using Psycopg2)
我有一个 Pandas 数据帧 df,我想将其作为模式存储在数据库中。 默认情况下,它将文本作为默认数据类型。 我使用 dtype=sqlalchemy.types.VARCHAR 参数将列保存为 VARCHAR。
df.to_sql('data', con, schema='schema', index= False, dtype=sqlalchemy.types.VARCHAR)
但是,我的一列的数据类型是多维“数组”。 我尝试了以下代码,但没有得到预期的输出。
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 '{}'");
上面的代码正在将所需的列转换为文本数组,但该数组为空。
我的数据如下所示:
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
我想将 col1 存储为 VARCHAR,将 col2 存储为大小为 n 的多维数组,存储每个部分由 ';' 分隔作为一个元素。
For A1:
array[1]: A1:DEF, Human
array[2]: X2:XYZ, Mouse
array[3]: Y1:RST, Rat
任何建议,如何使它工作?
谢谢
使用函数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 '{}';
如果你真的想得到多维数组,接下来更新表:
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.