【发布时间】:2018-04-06 15:18:56
【问题描述】:
我正在尝试在 python 中整理一个大 (8gb) .csv 文件,然后将其流式传输到 BigQuery。我下面的代码开始正常,因为创建了表并进入了前 1000 行,但随后出现错误:
InvalidSchema: Please verify that the structure and data types in the DataFrame match the schema of the destination table.
这可能与流缓冲区有关吗?我的问题是我需要在再次运行代码之前删除表,否则前 1000 个条目将由于“追加”方法而重复。
import pandas as pd
destination_table = 'product_data.FS_orders'
project_id = '##'
pkey ='##'
chunks = []
for chunk in pd.read_csv('Historic_orders.csv',chunksize=1000, encoding='windows-1252', names=['Orderdate','Weborderno','Productcode','Quantitysold','Paymentmethod','ProductGender','DeviceType','Brand','ProductDescription','OrderType','ProductCategory','UnitpriceGBP' 'Webtype1','CostPrice','Webtype2','Webtype3','Variant','Orderlinetax']):
chunk.replace(r' *!','Null', regex=True)
chunk.to_gbq(destination_table, project_id, if_exists='append', private_key=pkey)
chunks.append(chunk)
df = pd.concat(chunks, axis=0)
print(df.head(5))
pd.to_csv('Historic_orders_cleaned.csv')
【问题讨论】:
-
你的csv文件可能有一些无效字符,比如非utf 8数据。
-
你为什么要播放它?加载作业会更好地使用现有的 csv
-
@FelipeHoffa 你知道是否可以使用 to_gbq 进行批处理吗?我该如何更改?