Sor*_*deh 9 dataframe python-3.x pandas google-bigquery google-cloud-datalab
我正在使用以下代码将具有多个NaN值的 Pandas 数据框插入到 BigQuery 表中。数据帧在云 Datalab 中准备。
import google.datalab.bigquery as bq
bqtable = ('project_name', 'dataset_name', 'table_name')
table = bq.Table(bqtable)
table_schema = bq.Schema.from_data(df)
table.create(schema = table_schema, overwrite = True)
table.insert(df)
Run Code Online (Sandbox Code Playgroud)
由于NaN数据框中的值,我收到以下错误:
RequestException: HTTP request failed: Invalid JSON payload received.
Unexpected token. : "user_id": NaN,
^
Run Code Online (Sandbox Code Playgroud)
我知道这JSON不明白,NaN但我不能只是fillna用来将这些NaN值转换为其他值,因为我需要将这些字段插入nullBigQuery 表中。有没有人有解决方法?
将所有np.nan值替换为 python 的None值,然后重新运行代码(或尝试df.to_gbq):
df = df.where(pd.notnull(df), None)
Run Code Online (Sandbox Code Playgroud)
我没有使用 Google BigQuery 的经验,并且我认为您现有的代码没有任何问题,但可能值得安装该pandas-gbq软件包。然后尝试使用 将 DataFrame 写入 GBQ df.to_gbq,详细信息请参阅此处的文档:https ://pandas.pydata.org/pandas-docs/stable/ generated/pandas.DataFrame.to_gbq.html
| 归档时间: |
|
| 查看次数: |
3192 次 |
| 最近记录: |