Python 3.x 获取错误请验证数据帧中的结构和数据类型在追加时是否与目标表的架构匹配
我正在运行一个循环,该循环将运行一些进程,创建一个数据帧,然后将该数据帧添加到大查询表中。但当我在现有表上追加时,我得到了一个错误。 请验证DataFrame中的结构和数据类型是否与目标表的架构匹配。 这些值来自函数Python 3.x 获取错误请验证数据帧中的结构和数据类型在追加时是否与目标表的架构匹配,python-3.x,pandas,google-bigquery,google-cloud-datalab,Python 3.x,Pandas,Google Bigquery,Google Cloud Datalab,我正在运行一个循环,该循环将运行一些进程,创建一个数据帧,然后将该数据帧添加到大查询表中。但当我在现有表上追加时,我得到了一个错误。 请验证DataFrame中的结构和数据类型是否与目标表的架构匹配。 这些值来自函数 from pandas.io import gbq import pandas as pd import numpy as np import datetime as dt from datalab.context import Context import time for
from pandas.io import gbq
import pandas as pd
import numpy as np
import datetime as dt
from datalab.context import Context
import time
for id_name in ID_:
df= ['id_recip','length_data','length_action', 'daily_mail_freq', 'weekly_mail_frequency', 'imp_hour', 'imp_day']
columns = list(df)
data=[]
values = [id_name,length_data,length_action, daily_mail, weekly_mail, imp_hour, imp_day]
zipped = zip(columns, values)
a_dictionary = dict(zipped)
print(a_dictionary)
final_output=pd.DataFrame(a_dictionary)
final_output = final_output.astype(str)
final_output.info()
final_output.to_gbq('internal.frequency_output3',
Context.default().project_id,
if_exists='append')
我将dataframe中的所有数据转换为字符串,以避免数据类型不匹配。如果第一个循环表不存在,将创建它
Structure in bigquery table
daily_mail_freq STRING NULLABLE
id_recip STRING NULLABLE
imp_day STRING NULLABLE
imp_hour STRING NULLABLE
length_action STRING NULLABLE
length_data STRING NULLABLE
weekly_mail_frequency STRING NULLABLE
其中没有日期一种方法是使用 google.CloudBigQuery。在这种情况下,它将更改为sql语句并推送数据,而不是使用dataframe
def export_items_to_bigquery(daily_mail_freq,id_recip,imp_day,imp_hour,length_action,length_data,weekly_mail_frequency ):
# Instantiates a client
client = bigquery.Client()
bigquery_client = bigquery.Client()
# Prepares a reference to the dataset
dataset_ref = bigquery_client.dataset('dbn')
table_ref = dataset_ref.table('fqo')
table = bigquery_client.get_table(table_ref)
rows_to_insert = [
(daily_mail_freq , id_recip, imp_day, imp_hour, length_action , length_data, weekly_mail_frequency)]
errors = bigquery_client.insert_rows(table, rows_to_insert) # API request
assert errors == []
现在在循环中,只需将数据传递给函数