[英]How to use Python to create a single dataset from hundreds of JSON files?
[英]How to create a table with hundreds of columns in Snowflake?
我在 Snowflake 上使用 Python,我需要將具有數百列的 pandas dataframe 移動到 Snowflake。 我不知道如何在雪花連接器 cursor 內的“ Create Table
...”中輸入每個列名及其數據類型的情況下這樣做。
有誰知道任何建議? 任何指導都會有所幫助。
謝謝
Pandas .toSql()
應該可以工作。 如果您嘗試過但沒有得到您想要的結果 - 請在您的問題中包含這些詳細信息。
以下可以創建具有正確架構的表:
df.head(0).to_sql(
'MY_TABLE', con=connection, if_exists="replace", index=False)
否則,請參見下文。
您不需要“鍵入”每一列及其數據類型,您可以使用 Python 生成一個create table
語句來匹配您的 dataframe。
我用 JS 做了類似的事情:
CREATE or replace PROCEDURE create_wide_table()
RETURNS VARCHAR
LANGUAGE javascript
AS
$$
ss = 'create or replace table wide2000 (id int';
for (const x of Array(2000).keys()) {
ss += ', a' +x + ' int default uniform(1, 10000, random())';
}
ss += ');'
//return ss;
var rs = snowflake.execute( { sqlText: ss } );
return 'Done.';
$$;
這將生成並執行一個 SQL 查詢,如下所示:
create or replace table wide2000
(id int
, a0 int default uniform(1, 10000, random())
, a1 int default uniform(1, 10000, random())
, ...
);
(來自https://stackoverflow.com/a/67132536/132438 )
在您的情況下,使用 Python 迭代 dataframe 列名稱和類型,並生成create table
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.