{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "from pandas import Series,DataFrame\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0 3\n", "1 6\n", "2 9\n", "3 12\n", "dtype: int64" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# numpy.arrayに似たSeriesをつくります。違いは、データにラベルがあるところ\n", "obj = Series([3,6,9,12])\n", "obj" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 3, 6, 9, 12])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# valuesで、値が返ってくる\n", "obj.values" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "Int64Index([0, 1, 2, 3], dtype='int64')" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# indexを表示\n", "obj.index" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "USSR 8700000\n", "Germany 4300000\n", "China 3000000\n", "Japan 2100000\n", "USA 400000\n", "dtype: int64" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#index付きのデータを作る\n", "#第二次世界大戦の死傷者\n", "ww2_cas = Series([8700000,4300000,3000000,2100000,400000],index=['USSR','Germany','China','Japan','USA'])\n", "ww2_cas" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "400000" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 文字列のindexでアクセスできる。\n", "ww2_cas['USA']" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "USSR 8700000\n", "Germany 4300000\n", "dtype: int64" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 400万人以上の死傷者を出したのは?\n", "ww2_cas[ww2_cas>4000000]" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 普通の辞書のように扱えます。\n", "# USSR があるか?\n", "'USSR' in ww2_cas" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{'China': 3000000,\n", " 'Germany': 4300000,\n", " 'Japan': 2100000,\n", " 'USA': 400000,\n", " 'USSR': 8700000}" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 辞書型に変換できます。\n", "ww2_dict = ww2_cas.to_dict()\n", "ww2_dict" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# 辞書をもとに、Seriesを作ることができます。\n", "WW2_Series = Series(ww2_dict)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "China 3000000\n", "Germany 4300000\n", "Japan 2100000\n", "USA 400000\n", "USSR 8700000\n", "dtype: int64" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "WW2_Series" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# indexを明示的に与えることができます。たとえば・・・\n", "countries = ['China','Germany','Japan','USA','USSR','Argentina']" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# 別のSeriesを作ります\n", "obj2 = Series(ww2_dict,index=countries)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "China 3000000\n", "Germany 4300000\n", "Japan 2100000\n", "USA 400000\n", "USSR 8700000\n", "Argentina NaN\n", "dtype: float64" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "obj2" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "China False\n", "Germany False\n", "Japan False\n", "USA False\n", "USSR False\n", "Argentina True\n", "dtype: bool" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# nullデータがあるかどうかを確認できます\n", "pd.isnull(obj2)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "China True\n", "Germany True\n", "Japan True\n", "USA True\n", "USSR True\n", "Argentina False\n", "dtype: bool" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 逆のこともできます。\n", "pd.notnull(obj2)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "China 3000000\n", "Germany 4300000\n", "Japan 2100000\n", "USA 400000\n", "USSR 8700000\n", "dtype: int64" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# もとのデータに戻りましょう。\n", "WW2_Series" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "China 3000000\n", "Germany 4300000\n", "Japan 2100000\n", "USA 400000\n", "USSR 8700000\n", "Argentina NaN\n", "dtype: float64" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# \n", "obj2" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "Argentina NaN\n", "China 6000000\n", "Germany 8600000\n", "Japan 4200000\n", "USA 800000\n", "USSR 17400000\n", "dtype: float64" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 両方のデータを足すと、pandasが自動的に、indexでまとめてくれます。\n", "WW2_Series + obj2" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Seriesに名前を付けられます。\n", "obj2.name = '第二次世界大戦の死傷者'\n", "# Python2の場合は、uが必要です。" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "China 3000000\n", "Germany 4300000\n", "Japan 2100000\n", "USA 400000\n", "USSR 8700000\n", "Argentina NaN\n", "Name: 第二次世界大戦の死傷者, dtype: float64" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "obj2" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# indexに名前を付けることも可能\n", "obj2.index.name = 'Countries'" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "Countries\n", "China 3000000\n", "Germany 4300000\n", "Japan 2100000\n", "USA 400000\n", "USSR 8700000\n", "Argentina NaN\n", "Name: 第二次世界大戦の死傷者, dtype: float64" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "obj2" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.4.3" } }, "nbformat": 4, "nbformat_minor": 0 }