{ "metadata": { "name": "", "signature": "sha256:d60699d2037465d2556437c80b211ae4c60dd1ad4e52e2029eca5468427fe64a" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Indexing And Slicing Numpy Arrays\n", "\n", "- **Author:** [Chris Albon](http://www.chrisalbon.com/), [@ChrisAlbon](https://twitter.com/chrisalbon)\n", "- **Date:** -\n", "- **Repo:** [Python 3 code snippets for data science](https://github.com/chrisalbon/code_py)\n", "- **Note:**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Import modules" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a 2x2 array" ] }, { "cell_type": "code", "collapsed": false, "input": [ "battle_deaths = [[344, 2345], [253, 4345]]\n", "deaths = np.array(battle_deaths)\n", "deaths" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 3, "text": [ "array([[5341, 2345],\n", " [ 253, 4345]])" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Select the top row, second item" ] }, { "cell_type": "code", "collapsed": false, "input": [ "deaths[0, 1]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ "2345" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Select the second column" ] }, { "cell_type": "code", "collapsed": false, "input": [ "deaths[:, 1]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 6, "text": [ "array([2345, 4345])" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Select the second row" ] }, { "cell_type": "code", "collapsed": false, "input": [ "deaths[1, :]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 7, "text": [ "array([ 253, 4345])" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create an array of civilian deaths" ] }, { "cell_type": "code", "collapsed": false, "input": [ "civilian_deaths = np.array([4352, 233, 3245, 256, 2394])\n", "civilian_deaths" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 23, "text": [ "array([4352, 233, 3245, 256, 2394])" ] } ], "prompt_number": 23 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Find the index of battles with less than 500 deaths" ] }, { "cell_type": "code", "collapsed": false, "input": [ "few_civ_deaths = np.where(civilian_deaths < 500)\n", "few_civ_deaths" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 24, "text": [ "(array([1, 3]),)" ] } ], "prompt_number": 24 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Find the number of civilian deaths in battles with less than 500 deaths" ] }, { "cell_type": "code", "collapsed": false, "input": [ "civ_deaths = civilian_deaths[few_civ_deaths]\n", "civ_deaths" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 22, "text": [ "array([233, 256])" ] } ], "prompt_number": 22 } ], "metadata": {} } ] }