{"id":1499,"date":"2020-05-27T23:15:16","date_gmt":"2020-05-27T14:15:16","guid":{"rendered":"https:\/\/obenkyolab.com\/?p=1499"},"modified":"2020-05-27T23:17:00","modified_gmt":"2020-05-27T14:17:00","slug":"python-dataframe%e3%81%8b%e3%82%89mysql%e3%81%abinsert%e3%81%99%e3%82%8b","status":"publish","type":"post","link":"https:\/\/obenkyolab.com\/?p=1499","title":{"rendered":"Python dataframe\u304b\u3089MySQL\u306binsert\u3059\u308b"},"content":{"rendered":"\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/obenkyolab.com\/?p=1499\/#%E7%9B%AE%E7%9A%84\" >\u76ee\u7684<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/obenkyolab.com\/?p=1499\/#%E7%92%B0%E5%A2%83\" >\u74b0\u5883<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/obenkyolab.com\/?p=1499\/#Step1_%E6%8C%BF%E5%85%A5%E5%85%88%E3%83%86%E3%83%BC%E3%83%96%E3%83%AB%E7%94%A8%E6%84%8F\" >Step1 : \u633f\u5165\u5148\u30c6\u30fc\u30d6\u30eb\u7528\u610f<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/obenkyolab.com\/?p=1499\/#Step2_%E6%8C%BF%E5%85%A5%E7%94%A8%E3%83%87%E3%83%BC%E3%82%BF%E6%BA%96%E5%82%99\" >Step2 : \u633f\u5165\u7528\u30c7\u30fc\u30bf\u6e96\u5099<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/obenkyolab.com\/?p=1499\/#Step3_MySQL%E3%81%B8%E3%81%AE%E6%8C%BF%E5%85%A5\" >Step3 : MySQL\u3078\u306e\u633f\u5165<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/obenkyolab.com\/?p=1499\/#%E8%A3%9C%E8%B6%B3\" >\u88dc\u8db3<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E7%9B%AE%E7%9A%84\"><\/span>\u76ee\u7684<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>python\u306edataframe\u306f\u30c7\u30fc\u30bf\u306e\u524d\u51e6\u7406\u306b\u5927\u5909\u4fbf\u5229\u3067\u3001\u524d\u51e6\u7406\u3057\u305f\u5f8c\u306b\u305d\u306e\u307e\u307eDB\u306b\u633f\u5165\u3057\u305f\u3044\u6642\u304c\u591a\u3005\u3042\u308a\u307e\u3059\u3002\u4eca\u56de\u306fMySQL\u3067\u8a66\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E7%92%B0%E5%A2%83\"><\/span>\u74b0\u5883<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Python 3.7.3<br>MySQL 5.5.62<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Step1_%E6%8C%BF%E5%85%A5%E5%85%88%E3%83%86%E3%83%BC%E3%83%96%E3%83%AB%E7%94%A8%E6%84%8F\"><\/span>Step1 : \u633f\u5165\u5148\u30c6\u30fc\u30d6\u30eb\u7528\u610f<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u30c6\u30b9\u30c8\u7528\u306e\u30c6\u30fc\u30d6\u30eb\u3092\u5b9a\u7fa9\u3057\u307e\u3059\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\n&gt; CREATE DATABASE testdb;\n&gt; CREATE TABLE testdb.mysql_table(\n\tcol1 int\n\t,col2 int\n\t,col3 int\n  );\n\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Step2_%E6%8C%BF%E5%85%A5%E7%94%A8%E3%83%87%E3%83%BC%E3%82%BF%E6%BA%96%E5%82%99\"><\/span>Step2 : \u633f\u5165\u7528\u30c7\u30fc\u30bf\u6e96\u5099<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Python\u304b\u3089\u633f\u5165\u3059\u308b\u305f\u3081\u306e\u30c7\u30fc\u30bf\u3092\u6e96\u5099\u3057\u307e\u3059\u3002pandas\u306eDataFrame\u3067\u633f\u5165\u5148\u30c6\u30fc\u30d6\u30eb\u306e\u578b\u306b\u3042\u3063\u305f\u30c7\u30fc\u30bf\u3092\u7528\u610f\u3057\u307e\u3059\u3002\u6ce8\u610f\u70b9\u3068\u3057\u3066\u3001\u30c7\u30fc\u30bf\u30d5\u30ec\u30fc\u30e0\u5074\u306b\u633f\u5165\u5148\u30ab\u30e9\u30e0\u3068\u540c\u3058\u540d\u79f0\u306e\u30ab\u30e9\u30e0\u540d\u3092\u30bb\u30c3\u30c8\u3057\u3066\u304a\u304b\u306a\u3044\u3068\u3001\u633f\u5165\u6642\u306b\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3057\u307e\u3059\u306e\u3067\u3001\u5fd8\u308c\u305a\u306b\u30bb\u30c3\u30c8\u3057\u307e\u3059\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport pandas as pd\ndf = pd.DataFrame(&#x5B;&#x5B;1,2,3],&#x5B;4,5,6]],columns=&#x5B;&#039;col1&#039;,&#039;col2&#039;,&#039;col3&#039;])\nprint(df)\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#\u7d50\u679c\n   0  1  2\n0  1  2  3\n1  4  5  6\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Step3_MySQL%E3%81%B8%E3%81%AE%E6%8C%BF%E5%85%A5\"><\/span>Step3 : MySQL\u3078\u306e\u633f\u5165<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u30c7\u30fc\u30bf\u30d5\u30ec\u30fc\u30e0\u578b\u306e\u30c7\u30fc\u30bf\u3092MySQL\u3078\u633f\u5165\u3059\u308b\u305f\u3081\u306bsqlalchemy\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002sqlalchemy\u306ecreate _engine\u30e1\u30bd\u30c3\u30c9\u3067\u63a5\u7d9a\u60c5\u5831\u3092\u5b9a\u7fa9\u3057\u307e\u3059\u3002user,password\u3084host,database\u306b\u3064\u3044\u3066\u306f\u63a5\u7d9a\u3059\u308bMySQL\u5074\u306e\u9069\u5207\u306a\u5024\u3092\u66f8\u304d\u307e\u3059\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom sqlalchemy import create_engine\n\n#engine = create_engine(&#039;mysql:\/\/&lt;user&gt;:&lt;password&gt;@&lt;host&gt;\/&lt;database&gt;?charset=utf8&#039;)\nengine = create_engine(&#039;mysql:\/\/user:password@localhost\/test_db?charset=utf8&#039;)\n\n#df.to_sql(&lt;table_name&gt;,con=engine, if_exists=&#039;append&#039;, index=False)\ndf.to_sql(&#039;testdb.mysq_table&#039;,con=engine, if_exists=&#039;append&#039;, index=False)\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#\u7d50\u679c\n   col1  col2  col3\n0     1     2     3\n1     4     5     6\n<\/pre><\/div>\n\n\n<p>\u7121\u4e8b\u306b\u633f\u5165\u3067\u304d\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E8%A3%9C%E8%B6%B3\"><\/span>\u88dc\u8db3<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>sqlalchemy\u3092\u4f7f\u7528\u3057\u3001to_sql\u3067\u633f\u5165\u3059\u308b\u65b9\u6cd5\u3067\u3059\u304c\u3001\u3082\u3057\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u5074\u306b\u30c6\u30fc\u30d6\u30eb\u304c\u5b58\u5728\u3057\u3066\u3044\u306a\u3044\u5834\u5408\u306f\u3001\u30c6\u30fc\u30d6\u30eb\u3092\u81ea\u52d5\u4f5c\u6210\u3057\u305f\u4e0a\u3067\u633f\u5165\u3057\u3066\u304f\u308c\u307e\u3059\u3002\u3053\u308c\u306f\u975e\u5e38\u306b\u4fbf\u5229\u3067\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u76ee\u7684 python\u306edataframe\u306f\u30c7\u30fc\u30bf\u306e\u524d\u51e6\u7406\u306b\u5927\u5909\u4fbf\u5229\u3067\u3001&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":1218,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_locale":"ja","_original_post":"https:\/\/obenkyolab.com\/?p=1499","footnotes":""},"categories":[7],"tags":[32],"class_list":["post-1499","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-32","ja"],"_links":{"self":[{"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/posts\/1499","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1499"}],"version-history":[{"count":7,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/posts\/1499\/revisions"}],"predecessor-version":[{"id":1506,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/posts\/1499\/revisions\/1506"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/media\/1218"}],"wp:attachment":[{"href":"https:\/\/obenkyolab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1499"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1499"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1499"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}