如何将包含列表的pandas数据帧上传到googlebigquery数组和structs格式?

2024-03-29 00:06:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用pandas将一个dataframe推入一个具有数组和结构的BigQuery表中

Bigquery表如下所示: 列a(int)、列b(string)、列c.a(具有数组子类型0的结构)、列c.b(具有int子类型的结构)

我有这样一个数据帧:

列a(类型int)、列b(字符串)、列c(对象列表)、列d(int)。你知道吗

有没有办法将数据帧上传到带有结构和数组的bigquery表?你知道吗


Tags: 数据对象字符串类型dataframepandas列表string
1条回答
网友
1楼 · 发布于 2024-03-29 00:06:28

是的,你能做到

import google.cloud
from google.cloud import bigquery


def save_df_to_bq(args,dataframe,csv_name):



print("Length of the incoming dataframe : >" , len(dataframe))

print(dataframe.iloc[[0]])

client = bigquery.Client()

dataset_ref = client.dataset(dataset_id,project_id)

 schema = [
     bigquery.SchemaField('col_a ', 'dttype'),
     bigquery.SchemaField('col_b ', 'dttype'),
     bigquery.SchemaField('col_c ', 'dttype'),

 ]      

acc_table_id = str(random.randint(100000,100000*1000000))
# table_ref = dataset_ref.table(acc_table_id)
# table = bigquery.Table(table_ref, schema=schema)
# table = client.create_table(table)  # API request
# print("Table created..")

# acc_table_id = str(random.randint(100000,100000*1000000))
acc_table_ref = client.dataset(dataset_id,project_id).table(acc_table_id)
job = client.load_table_from_dataframe(dataframe, acc_table_ref, location="US")
job.result()

相关问题 更多 >