{"templateId":"markdown","sharedDataIds":{"sidebar":"sidebar-@l10n/ja/sidebars.yaml"},"props":{"metadata":{"markdoc":{"tagList":[]},"type":"markdown"},"seo":{"title":"Treasure Data 用 Pandas と Jupyter の設定","description":"Treasure Data Product Documentation · Collect and Unify · Segment and Activate · Experiment and Analyze · Decisioning Automate with AI Scale and Trust.","siteUrl":"https://docs.treasuredata.com","lang":"en-US","llmstxt":{"hide":false,"sections":[{"title":"Table of contents","includeFiles":["**/*"],"excludeFiles":[]}],"excludeFiles":[]}},"dynamicMarkdocComponents":[],"compilationErrors":[],"ast":{"$$mdtype":"Tag","name":"article","attributes":{},"children":[{"$$mdtype":"Tag","name":"Heading","attributes":{"level":1,"id":"treasure-data-用-pandas-と-jupyter-の設定","__idx":0},"children":["Treasure Data 用 Pandas と Jupyter の設定"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Treasure Data は、SQL 経由でアクセス可能なクラウドベースの分析インフラストラクチャを提供しています。",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"/products/customer-data-platform/data-workbench/queries/trino/quickstart"},"children":["Trino"]}," のようなインタラクティブエンジンを使用すると、数十億のレコードを簡単に処理できます。ただし、データサイエンティストにとって SQL クエリを書くのは時に苦痛であり、結果を視覚化するには Excel や Tableau などの外部ツールを使用する必要があります。Treasure Data を Python ベースのデータ分析ツールである ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"http://pandas.pydata.org/"},"children":["Pandas"]}," と組み合わせて使用し、",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"https://jupyter.org/"},"children":["Jupyter Notebook"]}," を介してデータをインタラクティブに視覚化できます。"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"img","attributes":{"src":"/assets/image-20190925-190645.bf0be2045c23219c01095f0673eacf916240b2b202f3bee7f426bd2d7a0d99c9.60bcc915.png","alt":""},"children":[]}]},{"$$mdtype":"Tag","name":"ul","attributes":{},"children":[{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"#prerequisites"},"children":["前提条件"]}]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"#set-treasure-data-api-key"},"children":["Treasure API キーの設定"]}]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"#set-treasure-data-api-endpoint"},"children":["Treasure API エンドポイントの設定"]}]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"#install-the-necessary-packages-and-configure-your-environment"},"children":["必要なパッケージのインストールと環境の設定"]}]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"#run-jupyter-and-create-your-first-notebook"},"children":["Jupyter の実行と最初のノートブックの作成"]}]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"#explore-data"},"children":["データの探索"]}]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"#running-a-query-in-jupyter"},"children":["Jupyter でのクエリの実行"]}]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"#sample-data"},"children":["サンプルデータ"]}]}]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"前提条件","__idx":1},"children":["前提条件"]},{"$$mdtype":"Tag","name":"ul","attributes":{},"children":[{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Python の基礎知識。"]}]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Treasure Data の基礎知識。"]}]}]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"treasure-api-キーの設定","__idx":2},"children":["Treasure API キーの設定"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Jupyter を起動する前に、マスター API キーを環境変数として設定します。マスター API KEY は、Treasure コンソール プロファイルから取得できます。"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"bash","header":{"controls":{"copy":{}}},"source":"$ export TD_API_KEY=\"1234/abcde...\"\n","lang":"bash"},"children":[]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Jupyter Notebook セルで次のようなコマンドを使用して環境変数を設定することもできます。"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"cmd","header":{"controls":{"copy":{}}},"source":"%env TD_API_KEY = \"123c/abcdefghjk...\"\n","lang":"cmd"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"treasure-api-エンドポイントの設定","__idx":3},"children":["Treasure API エンドポイントの設定"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["アカウントが US リージョンに属していない場合は、Treasure API エンドポイントを環境変数として設定します。エンドポイント情報は",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"/apis/endpoints/endpoints"},"children":["こちら"]},"で確認できます。"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"bash","header":{"controls":{"copy":{}}},"source":"$ export TD_API_SERVER=\"https://api.treasuredata.co.jp\"\n","lang":"bash"},"children":[]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Jupyter Notebook セルで次のようなコマンドを使用して環境変数を設定することもできます。"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"cmd","header":{"controls":{"copy":{}}},"source":"%env TD_API_SERVER = \"https://api.treasuredata.co.jp\"\n","lang":"cmd"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"必要なパッケージのインストールと環境の設定","__idx":4},"children":["必要なパッケージのインストールと環境の設定"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["詳細情報と手順については、Conda、Pandas、matplotlib、Jupyter Notebook、pytd の",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"/ja/tools/cli-and-sdks/installing-python-pandas-matplotlib-jupyter-notebook-pytd"},"children":["インストール"]},"を参照してください。"]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"jupyter-の実行と最初のノートブックの作成","__idx":5},"children":["Jupyter の実行と最初のノートブックの作成"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["分析プロジェクトのフロントエンドとして Jupyter を使用します。"]},{"$$mdtype":"Tag","name":"ol","attributes":{},"children":[{"$$mdtype":"Tag","name":"li","attributes":{},"children":["次の構文を使用して Notebook を実行します:"]}]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"bash","header":{"controls":{"copy":{}}},"source":"(analysis)$ ipython notebook\n","lang":"bash"},"children":[]},{"$$mdtype":"Tag","name":"ol","attributes":{"start":2},"children":[{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Web ブラウザが開きます:"]}]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["New"]}," > ",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["Python 3"]}," を選択します。"]}]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"p","attributes":{},"children":["次のテキストをノートブックにコピーして貼り付けます:"]}]}]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"python","header":{"controls":{"copy":{}}},"source":"%matplotlib inline\n\nimport os\nimport pandas as pd\nimport pytd.pandas_td as td\n# Initialize the connection to Treasure Data\n\ncon = td.connect(apikey=os.environ['TD_API_KEY'], endpoint='https://api.treasuredata.com')\n","lang":"python"},"children":[]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"img","attributes":{"src":"/assets/image-20190925-190918.5bf9b8848977614d4845d71e9ff6ccf5b502fde7ab1270be7a6ef7c962f9a063.60bcc915.png","alt":""},"children":[]}]},{"$$mdtype":"Tag","name":"ol","attributes":{"start":5},"children":[{"$$mdtype":"Tag","name":"li","attributes":{},"children":["ノートブックは次のようになります"]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"img","attributes":{"src":"/assets/jupyter_pytd.c691f29202d2b11bce5d2b9dd9f8a9dd71b237a44272834e5a30d7c7707ac0b2.60bcc915.png","alt":""},"children":[]}," ","6. Shift-Enter を押します。"," ","\"KeyError: 'TD_API_KEY'\" エラーが発生した場合は、\"apikey=os.environ['TD_API_KEY']\" の代わりに ",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["\"apikey='your master apikey'\""]}," を試してください。"," ","動作する場合、Jupyter は OS からの TD_API_KEY 変数を認識していません。"," ","TD_API_KEY を再度確認し、Jupyter を再起動してください。"]},{"$$mdtype":"Tag","name":"ol","attributes":{"start":7},"children":[{"$$mdtype":"Tag","name":"li","attributes":{},"children":["オプションで、ノートブックを保存します。"]}]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"データの探索","__idx":6},"children":["データの探索"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["sample_datasets"]}," には2つのテーブルがあります。magic コマンド ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["td_tables"]}," を使用して、データベース内のすべてのテーブルを表示できます。"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"img","attributes":{"src":"/assets/image-20200421-215102.77c3d435e3eabf4495b11e93741de1a1e7986434701583e6e6ad1640c6185e11.60bcc915.png","alt":""},"children":[]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["nasdaq"]}," テーブルを探索しましょう。"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Jupyter で、次の構文を入力します:"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"python","header":{"controls":{"copy":{}}},"source":"engine = td.create_engine(\"presto:sample_datasets\")\nclient =td.Client(database='sample_datasets')\nclient.query('select symbol, count(1) as cnt from nasdaq group by 1 order by 1')\n","lang":"python"},"children":[]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["例:"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"img","attributes":{"src":"/assets/image-20200418-223403.5f2a746f8f0485079a18bc0eb7633bbc08f8e4faf276f2eb35d5e94fb17824e8.60bcc915.png","alt":""},"children":[]}]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"jupyter-でのクエリの実行","__idx":7},"children":["Jupyter でのクエリの実行"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["この例の目的上、Trino がこのセッションのクエリエンジンとして使用されます。"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Jupyter で、次の構文を入力します:"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"python","header":{"controls":{"copy":{}}},"source":"import pytd.pandas_td as td\ncon = td.connect(apikey=apikey, endpoint=\"https://api.treasuredata.com\")\nengine = td.create_engine(\"presto:sample_datasets\")\ntd.read_td_query(query, engine, index_col=None, parse_dates=None, distributed_join=False, params=None)\n","lang":"python"},"children":[]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["time_range"]}," パラメータを使用して、特定の時間範囲内のデータを取得することもできます:"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"img","attributes":{"src":"/assets/image-20190925-191519.1d48dd56769110424b6dd90d31037b2a5ac0bb5e1a85fa4ebed0e7fc2fdf4fe8.60bcc915.png","alt":""},"children":[]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["データは DataFrame としてローカル変数 ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["df"]}," に格納されます。データはコンピュータのローカルメモリにあるため、Pandas と Jupyter の力を使ってインタラクティブに分析できます。時系列データの詳細については、",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"http://pandas.pydata.org/pandas-docs/stable/timeseries.html"},"children":["Time Series / Date functionality"]}," を参照してください。"]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"サンプルデータ","__idx":8},"children":["サンプルデータ"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["データセットが非常に大きくなると、前のステップの方法はあまり拡張性がありません。メモリの制限や遅いネットワーク転送のため、一度に数百万行以上を取得することはお勧めしません。大量のデータを分析する場合は、転送されるデータ量を制限する必要があります。"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["これを行うには2つの方法があります:"]},{"$$mdtype":"Tag","name":"ul","attributes":{},"children":[{"$$mdtype":"Tag","name":"li","attributes":{},"children":["データをサンプリングできます。たとえば、「Nasdaq」テーブルには 8,807,278 行があります。100000 の制限を設定すると 100,000 行になり、これは取得するのに妥当なサイズです:"]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"img","attributes":{"src":"/assets/image-20190925-191603.2c0d4d1eb14b4e64f49a6c2d7405f83d6f7fd3f86e0694478061590cb60f167b.60bcc915.png","alt":""},"children":[]}]},{"$$mdtype":"Tag","name":"ul","attributes":{},"children":[{"$$mdtype":"Tag","name":"li","attributes":{},"children":["SQL を記述してサーバー側からデータを制限します。たとえば、「AAPL」に関連するデータのみに興味がある場合、",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["read_td_query"]}," を使用してレコード数をカウントしましょう:"]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"img","attributes":{"src":"/assets/image-20190925-191649.7c74860a4312c5f17c87b0b4eb22890b8c92e0a96723b5b48d7786c8f409775a.60bcc915.png","alt":""},"children":[]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["十分に小さいので、すべての行を取得してデータの分析を開始できます:"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"img","attributes":{"src":"/assets/image-20190925-191754.2d7f06efaab93a3ebf0d9f947587125e7857504afc88bcde007e8303c9a9105d.60bcc915.png","alt":""},"children":[]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["詳細については、以下の内容を参照してください。"]},{"$$mdtype":"Tag","name":"ul","attributes":{},"children":[{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"http://shop.oreilly.com/product/0636920023784.do"},"children":["Python for Data Analysis (O'Reilly Media の書籍)"]}]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Jupyter Notebook は GitHub でサポートされており、分析セッションの結果をチームと共有できます:"]},{"$$mdtype":"Tag","name":"ul","attributes":{},"children":[{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"https://github.com/blog/1995-github-jupyter-notebooks-3"},"children":["GitHub + Jupyter Notebooks = <3"]}]}]}]},"headings":[{"value":"Treasure Data 用 Pandas と Jupyter の設定","id":"treasure-data-用-pandas-と-jupyter-の設定","depth":1},{"value":"前提条件","id":"前提条件","depth":2},{"value":"Treasure API キーの設定","id":"treasure-api-キーの設定","depth":2},{"value":"Treasure API エンドポイントの設定","id":"treasure-api-エンドポイントの設定","depth":2},{"value":"必要なパッケージのインストールと環境の設定","id":"必要なパッケージのインストールと環境の設定","depth":2},{"value":"Jupyter の実行と最初のノートブックの作成","id":"jupyter-の実行と最初のノートブックの作成","depth":2},{"value":"データの探索","id":"データの探索","depth":2},{"value":"Jupyter でのクエリの実行","id":"jupyter-でのクエリの実行","depth":2},{"value":"サンプルデータ","id":"サンプルデータ","depth":2}],"frontmatter":{"seo":{"title":"Treasure Data 用 Pandas と Jupyter の設定"}},"lastModified":"2026-06-01T09:34:27.000Z","pagePropGetterError":{"message":"","name":""}},"slug":"/ja/tools/cli-and-sdks/pandas-and-jupyter-configuration-for-treasure-data","userData":{"isAuthenticated":false,"teams":["anonymous"]},"isPublic":true}