{"id":4429,"date":"2024-01-31T18:27:19","date_gmt":"2024-01-31T09:27:19","guid":{"rendered":"https:\/\/blog.since2020.jp\/?p=4429"},"modified":"2024-01-31T18:28:52","modified_gmt":"2024-01-31T09:28:52","slug":"python%e3%81%8b%e3%82%89bigquey%e3%81%b8%e5%88%97%e5%90%8d%e3%81%a8%e3%83%87%e3%83%bc%e3%82%bf%e5%9e%8b%e6%8c%87%e5%ae%9a%e3%81%aa%e3%81%97%e3%81%a7%e3%82%a2%e3%83%83%e3%83%97%e3%83%ad%e3%83%bc","status":"publish","type":"post","link":"https:\/\/since2020.jp\/media\/python%e3%81%8b%e3%82%89bigquey%e3%81%b8%e5%88%97%e5%90%8d%e3%81%a8%e3%83%87%e3%83%bc%e3%82%bf%e5%9e%8b%e6%8c%87%e5%ae%9a%e3%81%aa%e3%81%97%e3%81%a7%e3%82%a2%e3%83%83%e3%83%97%e3%83%ad%e3%83%bc\/","title":{"rendered":"Python\u304b\u3089BigQuey\u3078\u5217\u540d\u3068\u30c7\u30fc\u30bf\u578b\u6307\u5b9a\u306a\u3057\u3067\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3059\u308b\u65b9\u6cd5"},"content":{"rendered":"\n<p>Python\u304b\u3089pandas\u30c7\u30fc\u30bf\u3092BigQuery\u306b\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3059\u308b\u969b\u306e\u5217\u540d\u3068\u30c7\u30fc\u30bf\u578b\u306e\u6307\u5b9a\u304c\u7169\u308f\u3057\u3044\u8ab2\u984c\u306b\u5bfe\u3059\u308b\u89e3\u6c7a\u7b56\u3092\u51fa\u3057\u307e\u3057\u305f\uff01\u30b3\u30fc\u30c9\u306e\u4f8b\u3092\u4ea4\u3048\u306a\u304c\u3089\u3001\u3053\u306e\u554f\u984c\u3092\u52b9\u7387\u7684\u306b\u89e3\u6c7a\u3059\u308b\u65b9\u6cd5\u3092\u7d39\u4ecb\u3057\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001BigQuery\u3078\u306e\u30c7\u30fc\u30bf\u306e\u79fb\u884c\u304c\u3088\u308a\u30b9\u30e0\u30fc\u30ba\u3067\u52b9\u7387\u7684\u306a\u3082\u306e\u3068\u306a\u308b\u3067\u3057\u3087\u3046\u3002<\/p>\n\n\n<h2>\u306f\u3058\u3081\u306b<\/h2>\n<p>Python\u304b\u3089pandas\u578b\u306e\u30c7\u30fc\u30bf\u3092BigQuery\u306b\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3059\u308b\u3068\u304d\u306b\u3001\u5217\u540d\u306e\u6307\u5b9a\u3068\u30c7\u30fc\u30bf\u578b\u306e\u6307\u5b9a\u3092\u3059\u308b\u306e\u304c\u3081\u3093\u3069\u304f\u3055\u304f\u306a\u3044\u3067\u3059\u304b\uff1f<\/p>\r\n<p>\u4eca\u56de\u306f\u305d\u306e\u554f\u984c\u3092\u89e3\u6d88\u3059\u308b\u30b3\u30fc\u30c9\u3092\u4f5c\u6210\u3057\u307e\u3057\u305f(with ChatGPT)\uff01<\/p>\r\n<p>\u3053\u308c\u3067\u304b\u306a\u308a\u6642\u77ed\u3067\u304d\u305f\u306e\u3067\u662f\u975e\u4f7f\u3063\u3066\u307f\u3066\u4e0b\u3055\u3044\u3002<\/p>\n\n<h2>GCP\u306eVertex AI Workbench\u3067\u958b\u767a<\/h2>\n<p>\u74b0\u5883\u306fGCP\u306eVertex AI Workbench\u3067\u3059\u3002<\/p>\r\n<p>\u3053\u306e\u74b0\u5883\u4ee5\u5916\u3060\u3068BigQuery\u3078\u306e\u8a8d\u8a3c\u306e\u6642\u306b\u5225\u306e\u624b\u9806\u3092\u8e0f\u307e\u306a\u304f\u3066\u306f\u3044\u3051\u307e\u305b\u3093\u3002<\/p>\r\n<p>\u3057\u304b\u3057\u3001GCP\u306eVertex AI \u306e\u30ef\u30fc\u30af\u30d9\u30f3\u30c1\u3060\u3068\u3081\u3061\u3083\u304f\u3061\u3083\u63a5\u7d9a\u304c\u697d\u3067\u3059\uff01\u3053\u308c\u304cGCP\u3067\u958b\u767a\u3059\u308b\u30e1\u30ea\u30c3\u30c8\u306e\u4e00\u3064\u3067\u3059\u306d\u3002<\/p>\n\n<h2>Python\u304b\u3089BigQuey\u3078\u5217\u540d\u3068\u30c7\u30fc\u30bf\u578b\u6307\u5b9a\u306a\u3057\u3067\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3059\u308b\u30b3\u30fc\u30c9<\/h2>\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>from google.cloud import bigquery\r\nimport pandas as pd\r\n\r\ndef upload_df_to_bigquery(df, destination_table, project_id):\r\nclient = bigquery.Client()\r\n\r\n# Get DataFrame dtypes\r\ndtypes = df.dtypes.reset_index()\r\n\r\n# Convert pandas dtypes to BigQuery schema\r\nschema = [bigquery.SchemaField(name, _pandas_type_to_bq_type(dtype)) for name, dtype in zip(dtypes['index'], dtypes[0])]\r\n\r\n# Specify job configuration\r\njob_config = bigquery.LoadJobConfig(\r\nschema=schema,\r\nwrite_disposition='WRITE_TRUNCATE' # Options: 'WRITE_TRUNCATE', 'WRITE_APPEND', 'WRITE_EMPTY'\r\n)\r\n\r\n# Upload DataFrame to BigQuery\r\nclient.load_table_from_dataframe(df, destination_table, job_config=job_config).result()\r\n\r\nprint('\u5b8c\u4e86')\r\n\r\ndef _pandas_type_to_bq_type(pandas_type):\r\nif pandas_type == 'object':\r\nreturn 'STRING'\r\nelif pandas_type == 'Int64':\r\nreturn 'INT64'\r\nelif pandas_type == 'float64':\r\nreturn 'FLOAT64'\r\nelif pandas_type == 'bool':\r\nreturn 'BOOL'\r\nelif pandas_type == 'datetime64[ns]':\r\nreturn 'TIMESTAMP'\r\nelse:\r\nreturn 'STRING' # Default to STRING if unknown type\r\n\r\nyour_dataframe = pd.read_csv('csv\u306e\u540d\u524d')\r\nproject_id = '\u30d7\u30ed\u30b8\u30a7\u30af\u30c8ID'\r\ntable_id = '\u30c6\u30fc\u30d6\u30ebID'\r\n\r\n# Example usage:\r\n# Replace 'your_project_id', 'your_dataset.your_table', 'your_dataframe', and 'path\/to\/your\/credentials.json' with actual values.\r\nupload_df_to_bigquery(your_dataframe,table_id, project_id)<\/code><\/pre>\r\n<p>upload_df_to_bigquery\u95a2\u6570\u306b\u3066\u3001\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3057\u305f\u3044\u30c7\u30fc\u30bf\u306e\u5834\u6240\u3092\u6307\u5b9a\u3057\u3066\u4e0b\u3055\u3044\u3002<\/p>\r\n<p>\u305d\u306e\u6b21\u306b_pandas_type_to_bq_type\u95a2\u6570\u3067pandas\u306e\u30c7\u30fc\u30bf\u578b\u3092\u81ea\u52d5\u3067BigQuery\u7528\u306b\u66f8\u304d\u63db\u3048\u3066\u304f\u308c\u307e\u3059\uff01\u3057\u304b\u3057\u3001\u3053\u306e\u30c7\u30fc\u30bf\u578b\u306b\u306a\u3044\u3082\u306e\u306f\u5909\u63db\u3067\u304d\u307e\u305b\u3093\u306e\u3067\u3001\u9069\u5b9c\u5fc5\u8981\u306a\u3082\u306e\u306f\u8ffd\u52a0\u3057\u3066\u4e0b\u3055\u3044\u3002<\/p>\r\n<\/div>\n\n<h2>\u307e\u3068\u3081\u3068\u6ce8\u610f\u70b9<\/h2>\n<p>\u4e0a\u306e\u30b3\u30fc\u30c9\u3092\u30b3\u30d4\u30fc\uff06\u30da\u30fc\u30b9\u30c8\u3059\u308b\u3060\u3051\u3067\u3061\u3083\u3061\u3083\u3063\u3068\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3067\u304d\u307e\u3059\uff01<\/p>\r\n<p>\u3082\u3057\u4e0a\u306e\u30c7\u30fc\u30bf\u578b\u306e\u6307\u5b9a\u3060\u3051\u3067\u4e0d\u5341\u5206\u306a\u5834\u5408\u306f\u62e1\u5f35\u3057\u3066\u4e0b\u3055\u3044\u3002<\/p>\r\n<p>\u6ce8\u610f\u70b9\u3068\u3057\u3066\u306fpandas\u3067\u6307\u5b9a\u3057\u3066\u3044\u308b\u30c7\u30fc\u30bf\u578b\u3068BigQuery\u3067\u4fdd\u5b58\u3057\u305f\u3044\u3068\u304d\u306e\u30c7\u30fc\u30bf\u578b\u304c\u7570\u306a\u308b\u6642\u306fpandas\u306e\u65b9\u304c\u53c2\u7167\u3055\u308c\u3066\u3057\u307e\u3046\u306e\u3067\u6ce8\u610f\u304c\u5fc5\u8981\u3067\u3059\u3002<\/p>","protected":false},"excerpt":{"rendered":"<p>Python\u304b\u3089pandas\u30c7\u30fc\u30bf\u3092BigQuery\u306b\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3059\u308b\u969b\u306e\u5217\u540d\u3068\u30c7\u30fc\u30bf\u578b\u306e\u6307\u5b9a\u304c\u7169\u308f\u3057\u3044\u8ab2\u984c\u306b\u5bfe\u3059\u308b\u89e3\u6c7a\u7b56\u3092\u51fa\u3057\u307e\u3057\u305f\uff01\u30b3\u30fc\u30c9\u306e\u4f8b\u3092\u4ea4\u3048\u306a\u304c\u3089\u3001\u3053\u306e\u554f\u984c\u3092\u52b9\u7387\u7684\u306b\u89e3\u6c7a\u3059\u308b\u65b9\u6cd5\u3092\u7d39\u4ecb\u3057\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001Bi [&hellip;]<\/p>\n","protected":false},"author":83,"featured_media":3123,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"content-type":"","swell_btn_cv_data":"","footnotes":"","_wp_rev_ctl_limit":""},"categories":[1246],"tags":[],"class_list":["post-4429","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-infrastructure"],"_links":{"self":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/posts\/4429","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/users\/83"}],"replies":[{"embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/comments?post=4429"}],"version-history":[{"count":1,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/posts\/4429\/revisions"}],"predecessor-version":[{"id":4430,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/posts\/4429\/revisions\/4430"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/media\/3123"}],"wp:attachment":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/media?parent=4429"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/categories?post=4429"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/tags?post=4429"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}