如何使用Pandas在Snowflake字符串列中插入NULL?

ippsafx7  于 2022-10-23  发布在  其他
关注(0)|答案(1)|浏览(145)

我正在使用pandas_tools包将数据写入我的Snowflake表。

create table TEST_TBL(
    ID integer,
    VALUE string
)
import pandas as pd
from snowflake.connector import pandas_tools as pt

df = pd.DataFrame(data={'ID': [1, 2],
                        'VALUE': ['test', None]
                       })

# assume everything else is set up previously

pt.write_pandas(conn=conn, 
                df=df, 
                table_name=table_name,
                database=database,
                schema=schema)

使用write_pandas(),而不是在ID=2的value列中插入NULL值(这是我使用python NoneType时所期望的情况),而是将文本字符串“None”写入表中。


小时
我也用np.nan尝试过,它还将文字字符串“NaN”写入列中。如何让它在记录中插入NULL值?

  • 另一个表的预期空输出示例*


小时

46qrfjad

46qrfjad1#

您的代码似乎正确。

import snowflake.connector
import pandas as pd
from snowflake.connector import pandas_tools as pt

conn = snowflake.connector.connect(**connection_parameters)
cursor = conn.cursor()
database = connection_parameters['database']
schema = connection_parameters['schema']
table_name = 'TEST_TBL'

sql = f'create table {table_name}(ID integer,VALUE string)'
cursor.execute(sql)

df = pd.DataFrame(data={'ID': [1, 2],
                        'VALUE': ['test', None]
                       })

# assume everything else is set up previously

pt.write_pandas(conn=conn, 
                df=df, 
                table_name=table_name,
                database=database,
                schema=schema)

sql = f'select * from {table_name}'
cursor.execute(sql)
for (ID, VALUE) in cursor:
   print('ID: {0}, VALUE: {1}'.format(ID, VALUE))
ID: 1, VALUE: test
ID: 2, VALUE: None


小时

相关问题