<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	>

<channel>
	<title>Plotly - MasaruのIT＆マーケティングBLOG | Info Tech &amp; Marketing BLOG by Masaru</title>
	<atom:link href="/tag/plotly/feed/" rel="self" type="application/rss+xml" />
	<link>/</link>
	<description>Cross the cliffs of the times</description>
	<lastBuildDate>Wed, 07 Apr 2021 11:04:11 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.8</generator>
<site xmlns="com-wordpress:feed-additions:1">191177362</site>	<item>
		<title>Jupyter NotebookからPythonでPostgreSQL 13を操作する</title>
		<link>/2021/04/07/jupyter-notebook%e3%81%8b%e3%82%89python%e3%81%a7postgresql-13%e3%82%92%e6%93%8d%e4%bd%9c%e3%81%99%e3%82%8b/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=jupyter-notebook%25e3%2581%258b%25e3%2582%2589python%25e3%2581%25a7postgresql-13%25e3%2582%2592%25e6%2593%258d%25e4%25bd%259c%25e3%2581%2599%25e3%2582%258b</link>
					<comments>/2021/04/07/jupyter-notebook%e3%81%8b%e3%82%89python%e3%81%a7postgresql-13%e3%82%92%e6%93%8d%e4%bd%9c%e3%81%99%e3%82%8b/#respond</comments>
		
		<dc:creator><![CDATA[masaru]]></dc:creator>
		<pubDate>Wed, 07 Apr 2021 10:49:51 +0000</pubDate>
				<category><![CDATA[IT]]></category>
		<category><![CDATA[データ分析]]></category>
		<category><![CDATA[Anaconda]]></category>
		<category><![CDATA[Jupyter Notebook]]></category>
		<category><![CDATA[Plotly]]></category>
		<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[PostgreSQL 13]]></category>
		<category><![CDATA[psycopg2]]></category>
		<category><![CDATA[Python]]></category>
		<guid isPermaLink="false">/?p=208</guid>

					<description><![CDATA[<p>まずは堅実に定番RDBから 　NoSQLブームで勉強会界隈では影が薄れた感があるRDBMSですが、当たり前の存在になっただけで健在です。当サイトが使っているCMS『WordPress』にはMySQLが組み込まれていますし&#8230; <a class="more-link" href="/2021/04/07/jupyter-notebook%e3%81%8b%e3%82%89python%e3%81%a7postgresql-13%e3%82%92%e6%93%8d%e4%bd%9c%e3%81%99%e3%82%8b/">続きを読む <span class="screen-reader-text">Jupyter NotebookからPythonでPostgreSQL 13を操作する</span></a></p>
<p>The post <a href="/2021/04/07/jupyter-notebook%e3%81%8b%e3%82%89python%e3%81%a7postgresql-13%e3%82%92%e6%93%8d%e4%bd%9c%e3%81%99%e3%82%8b/">Jupyter NotebookからPythonでPostgreSQL 13を操作する</a> first appeared on <a href="/">MasaruのIT＆マーケティングBLOG  |  Info Tech & Marketing BLOG by Masaru</a>.</p>]]></description>
										<content:encoded><![CDATA[<h2>まずは堅実に定番RDBから</h2>



<p>　NoSQLブームで勉強会界隈では影が薄れた感があるRDBMSですが、当たり前の存在になっただけで健在です。当サイトが使っているCMS『WordPress』にはMySQLが組み込まれていますし、当サイトを置いているAWSでも<a href="https://aws.amazon.com/jp/blogs/news/accelerate-amazon-redshift-federated-query-adoption-with-aws-cloudformation/" target="_blank" rel="noreferrer noopener">PostgreSQLがRedShiftやAuroraなど基幹サービスで使われています</a>。</p>



<figure class="wp-block-image size-large"><img loading="lazy" width="731" height="486" src="https://i0.wp.com/can.ne.jp/wp-content/uploads/2021/04/AccelerateRedshiftFederatedQuery1.png?resize=731%2C486&#038;ssl=1" alt="" class="wp-image-291" srcset="https://i0.wp.com/can.ne.jp/wp-content/uploads/2021/04/AccelerateRedshiftFederatedQuery1.png?w=731&amp;ssl=1 731w, https://i0.wp.com/can.ne.jp/wp-content/uploads/2021/04/AccelerateRedshiftFederatedQuery1.png?resize=300%2C199&amp;ssl=1 300w" sizes="(max-width: 731px) 100vw, 731px" data-recalc-dims="1" /></figure>



<p>　統計モデリングを知らないのにディープラーニングでイキる人が信用出来ないのと同様、RDBMSを知らないのにNoSQLを語るのもイタい人です。私も昨年は<a href="https://www.elastic.co/jp/elasticsearch/" target="_blank" rel="noreferrer noopener">Elasticsearch</a>などのNoSQLや<a href="https://ignite.apache.org/" target="_blank" rel="noreferrer noopener">Apache Ignite</a>などの分散データベースにハマっていましたが、自分のイタさに気づいたためPostgreSQLを学び直しています。</p>



<p>　NoSQLや分散データベースを業務で使いこなしている方々の講演は勉強会で聞くことが出来ますが、彼らは「RDBMSを経験した上でNoSQLに進んでいる」のです。未経験者が講演を聞いていきなりNoSQLに行くのは『悪手』です。</p>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h2>初心者がNoSQLや分散RDBでやりたいことはPostgreSQLでも出来ている</h2>



<p>　NoSQLが主に扱うJSONはPosgreSQLでも『<a href="https://www.postgresql.org/docs/13/datatype-json.html" target="_blank" rel="noreferrer noopener">JSON型</a>』として扱えます。IMDBで話題となったインメモリ処理はPostgreSQLでも<a href="https://techblog.gmo-ap.jp/2020/11/25/postgresql_memory_architecture/" target="_blank" rel="noreferrer noopener">駆使されています</a>。分散データベースの特徴であるクエリの並列処理はPostgreSQLでも<a href="https://oss-db.jp/dojo/dojo_column_04" target="_blank" rel="noreferrer noopener">パラレルクエリ</a>として実装されている上、特別の設定無しで自動的に使ってくれます。</p>



<p>　昨年、実際にApage IgniteでPCサーバ5台のクラスタを構築しましたが、数10GB程度のデータではクエリに要する時間がシングルノードのPostgreSQLの方が速かったです。2.5GbEでもLANの遅延で並列処理のメリットが相殺されてしまいます。この規模のデータでクエリを高速化するなら、<a href="https://www.tsukumo.co.jp/bto/pc/workstation/2021/WA9A-G210WT.html" target="_blank" rel="noreferrer noopener">32コア64スレッドのパソコン</a>でも買えば良いと思います。</p>



<p>　こういう事実は、基本的すぎて勉強会ではなかなか教えてもらえないですね。エンジニアの方にとっては常識なのでしょうが……<img src="https://s.w.org/images/core/emoji/13.1.0/72x72/1f4a6.png" alt="💦" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h2>PythonでPostgreSQLを扱うパッケージ『psycopg2』</h2>



<p>　PythonでPostgreSQLを扱う際は『psycopg2』というパッケージを使うのが一般的なようです。視覚化のためのパッケージ『Plotly』と併せてインストールします。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>(base) masaru@ASUS-TUF-Gaming:~$ conda install psycopg2
(base) masaru@ASUS-TUF-Gaming:~$ conda install plotly</code></pre></div>



<h2>Jupyter Notebookでの実装</h2>



<p>　必要なパッケージの読み込みなどの初期設定を行います。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-python" data-lang="Python"><code># -*- coding: utf-8 -*-
import psycopg2
import pandas as pd
import plotly as py
import plotly.graph_objs as go
from plotly.offline import iplot, init_notebook_mode
init_notebook_mode()</code></pre></div>



<p>　psychopg2でデータベース接続を定義します。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-python" data-lang="Python"><code>def connect():
    con = psycopg2.connect(&quot;host=&quot; + &quot;localhost&quot; +
                           &quot; port=&quot; + &quot;5432&quot; +
                           &quot; dbname=&quot; + &quot;google_mobility&quot; +
                           &quot; user=&quot; + &quot;masaru&quot; +
                           &quot; password=&quot; + &quot;xxxxxxxxxxxx&quot;)
    return con</code></pre></div>



<p>　続いて、クエリを定義します。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-python" data-lang="Python"><code>def select_execute(con, sql):
    with con.cursor() as cur:
        cur.execute(sql)
        rows = cur.fetchall()

    return rows</code></pre></div>



<p>　定義したクエリを実行します。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-python" data-lang="Python"><code>con = connect()
sql =  &quot;select * from google_mobility where SUB_REGION_1 = &#39;Tokyo&#39;&quot;
result = select_execute(con, sql)</code></pre></div>



<p>　クエリの結果をPandasデータフレームに代入します。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-python" data-lang="Python"><code>df = pd.DataFrame(result)
df.head()</code></pre></div>



<figure class="wp-block-image size-large"><img loading="lazy" width="737" height="159" src="https://i2.wp.com/can.ne.jp/wp-content/uploads/2021/04/pypg06.png?resize=737%2C159&#038;ssl=1" alt="" class="wp-image-293" srcset="https://i2.wp.com/can.ne.jp/wp-content/uploads/2021/04/pypg06.png?w=737&amp;ssl=1 737w, https://i2.wp.com/can.ne.jp/wp-content/uploads/2021/04/pypg06.png?resize=300%2C65&amp;ssl=1 300w" sizes="(max-width: 737px) 100vw, 737px" data-recalc-dims="1" /></figure>



<p>　とりあえず全部カラム名をつけてあげます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-python" data-lang="Python"><code>columns = [&quot;id&quot;,&quot;country_region_code&quot;,&quot;country_region&quot;,&quot;sub_region_1&quot;,&quot;sub_region_2&quot;,&quot;metro_area&quot;,&quot;iso_3166_2_code&quot;,&quot;census_fips_code&quot;,&quot;date&quot;,&quot;retail&quot;,&quot;grocery&quot;,&quot;parks&quot;,&quot;transit&quot;,&quot;workplaces&quot;,&quot;residental&quot;,&quot;place_id&quot;]
df.columns = columns
df.head()</code></pre></div>



<figure class="wp-block-image size-large"><img loading="lazy" width="750" height="132" src="https://i2.wp.com/can.ne.jp/wp-content/uploads/2021/04/pypg09.png?resize=750%2C132&#038;ssl=1" alt="" class="wp-image-295" srcset="https://i2.wp.com/can.ne.jp/wp-content/uploads/2021/04/pypg09.png?w=996&amp;ssl=1 996w, https://i2.wp.com/can.ne.jp/wp-content/uploads/2021/04/pypg09.png?resize=300%2C53&amp;ssl=1 300w, https://i2.wp.com/can.ne.jp/wp-content/uploads/2021/04/pypg09.png?resize=768%2C135&amp;ssl=1 768w" sizes="(max-width: 750px) 100vw, 750px" data-recalc-dims="1" /></figure>



<p>　Plotlyで表示するデータを設定します。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-python" data-lang="Python"><code>trace1 = go.Scatter(
        x = list(df.date),
        y = list(df.retail),
        mode = &#39;lines+markers&#39;,
        name = &#39;retail&#39;,
        marker = dict(
                color = &#39;blue&#39;
                )
        )

trace2 = go.Scatter(
        x = list(df.date),
        y = list(df.grocery),
        mode = &#39;lines+markers&#39;,
        name = &#39;grocery&#39;,
        marker = dict(
                color = &#39;orange&#39;
                )
        )

data = [trace1, trace2]</code></pre></div>



<p>　続いて、Plotlyの特長であるスライダーを設定します。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-python" data-lang="Python"><code>layout = dict(
    title=&#39;Time series with range slider and selectors&#39;,
    xaxis=dict(
        rangeselector=dict(
            buttons=list([
                dict(count=1,
                     label=&#39;1m&#39;,
                     step=&#39;month&#39;,
                     stepmode=&#39;backward&#39;),
                dict(count=6,
                     label=&#39;6m&#39;,
                     step=&#39;month&#39;,
                     stepmode=&#39;backward&#39;),
                dict(count=1,
                    label=&#39;YTD&#39;,
                    step=&#39;year&#39;,
                    stepmode=&#39;todate&#39;),
                dict(count=1,
                    label=&#39;1y&#39;,
                    step=&#39;year&#39;,
                    stepmode=&#39;backward&#39;),
                dict(step=&#39;all&#39;)
            ])
        ),
        rangeslider=dict(
            visible = True
        ),
        type=&#39;date&#39;
    )
)</code></pre></div>



<p>　グラフを描画します。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-python" data-lang="Python"><code>fig = dict(data=data, layout=layout)
iplot(fig)</code></pre></div>



<figure class="wp-block-image size-large"><img loading="lazy" width="750" height="348" src="https://i2.wp.com/can.ne.jp/wp-content/uploads/2021/04/pypg13.png?resize=750%2C348&#038;ssl=1" alt="" class="wp-image-296" srcset="https://i2.wp.com/can.ne.jp/wp-content/uploads/2021/04/pypg13.png?w=945&amp;ssl=1 945w, https://i2.wp.com/can.ne.jp/wp-content/uploads/2021/04/pypg13.png?resize=300%2C139&amp;ssl=1 300w, https://i2.wp.com/can.ne.jp/wp-content/uploads/2021/04/pypg13.png?resize=768%2C357&amp;ssl=1 768w" sizes="(max-width: 750px) 100vw, 750px" data-recalc-dims="1" /></figure>



<p>　スライダーを動かせるグラフの出来上がりです。</p>
		<div class="wpulike wpulike-default " ><div class="wp_ulike_general_class wp_ulike_is_not_liked"><button type="button"
					aria-label="いいねボタン"
					data-ulike-id="208"
					data-ulike-nonce="c61a6e13bb"
					data-ulike-type="post"
					data-ulike-template="wpulike-default"
					data-ulike-display-likers=""
					data-ulike-likers-style="popover"
					class="wp_ulike_btn wp_ulike_put_image wp_post_btn_208"></button><span class="count-box wp_ulike_counter_up" data-ulike-counter-value="0"></span>			</div></div>
	<p><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=%2F2021%2F04%2F07%2Fjupyter-notebook%25e3%2581%258b%25e3%2582%2589python%25e3%2581%25a7postgresql-13%25e3%2582%2592%25e6%2593%258d%25e4%25bd%259c%25e3%2581%2599%25e3%2582%258b%2F&amp;linkname=Jupyter%20Notebook%E3%81%8B%E3%82%89Python%E3%81%A7PostgreSQL%2013%E3%82%92%E6%93%8D%E4%BD%9C%E3%81%99%E3%82%8B" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_line" href="https://www.addtoany.com/add_to/line?linkurl=%2F2021%2F04%2F07%2Fjupyter-notebook%25e3%2581%258b%25e3%2582%2589python%25e3%2581%25a7postgresql-13%25e3%2582%2592%25e6%2593%258d%25e4%25bd%259c%25e3%2581%2599%25e3%2582%258b%2F&amp;linkname=Jupyter%20Notebook%E3%81%8B%E3%82%89Python%E3%81%A7PostgreSQL%2013%E3%82%92%E6%93%8D%E4%BD%9C%E3%81%99%E3%82%8B" title="Line" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=%2F2021%2F04%2F07%2Fjupyter-notebook%25e3%2581%258b%25e3%2582%2589python%25e3%2581%25a7postgresql-13%25e3%2582%2592%25e6%2593%258d%25e4%25bd%259c%25e3%2581%2599%25e3%2582%258b%2F&amp;linkname=Jupyter%20Notebook%E3%81%8B%E3%82%89Python%E3%81%A7PostgreSQL%2013%E3%82%92%E6%93%8D%E4%BD%9C%E3%81%99%E3%82%8B" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_facebook_messenger" href="https://www.addtoany.com/add_to/facebook_messenger?linkurl=%2F2021%2F04%2F07%2Fjupyter-notebook%25e3%2581%258b%25e3%2582%2589python%25e3%2581%25a7postgresql-13%25e3%2582%2592%25e6%2593%258d%25e4%25bd%259c%25e3%2581%2599%25e3%2582%258b%2F&amp;linkname=Jupyter%20Notebook%E3%81%8B%E3%82%89Python%E3%81%A7PostgreSQL%2013%E3%82%92%E6%93%8D%E4%BD%9C%E3%81%99%E3%82%8B" title="Messenger" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_copy_link" href="https://www.addtoany.com/add_to/copy_link?linkurl=%2F2021%2F04%2F07%2Fjupyter-notebook%25e3%2581%258b%25e3%2582%2589python%25e3%2581%25a7postgresql-13%25e3%2582%2592%25e6%2593%258d%25e4%25bd%259c%25e3%2581%2599%25e3%2582%258b%2F&amp;linkname=Jupyter%20Notebook%E3%81%8B%E3%82%89Python%E3%81%A7PostgreSQL%2013%E3%82%92%E6%93%8D%E4%BD%9C%E3%81%99%E3%82%8B" title="Copy Link" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=%2F2021%2F04%2F07%2Fjupyter-notebook%25e3%2581%258b%25e3%2582%2589python%25e3%2581%25a7postgresql-13%25e3%2582%2592%25e6%2593%258d%25e4%25bd%259c%25e3%2581%2599%25e3%2582%258b%2F&#038;title=Jupyter%20Notebook%E3%81%8B%E3%82%89Python%E3%81%A7PostgreSQL%2013%E3%82%92%E6%93%8D%E4%BD%9C%E3%81%99%E3%82%8B" data-a2a-url="/2021/04/07/jupyter-notebook%e3%81%8b%e3%82%89python%e3%81%a7postgresql-13%e3%82%92%e6%93%8d%e4%bd%9c%e3%81%99%e3%82%8b/" data-a2a-title="Jupyter NotebookからPythonでPostgreSQL 13を操作する"></a></p><p>The post <a href="/2021/04/07/jupyter-notebook%e3%81%8b%e3%82%89python%e3%81%a7postgresql-13%e3%82%92%e6%93%8d%e4%bd%9c%e3%81%99%e3%82%8b/">Jupyter NotebookからPythonでPostgreSQL 13を操作する</a> first appeared on <a href="/">MasaruのIT＆マーケティングBLOG  |  Info Tech & Marketing BLOG by Masaru</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>/2021/04/07/jupyter-notebook%e3%81%8b%e3%82%89python%e3%81%a7postgresql-13%e3%82%92%e6%93%8d%e4%bd%9c%e3%81%99%e3%82%8b/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">208</post-id>	</item>
	</channel>
</rss>
