{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "%pylab inline\n", "import pandas as pd" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "transactions_text = \"\"\"\\\n", "trip,category,amount\n", "Boston,Airfare,560\n", "Boston,Meals,25\n", "Boston,Meals,35\n", "Boston,Meals,20\n", "Boston,Hotel,450\n", "New York,Airfare,670\n", "Atlanta,Airfare,340\n", "New York,Meals,60\n", "New York,Meals,55\n", "New York,Hotel,250\n", "Atlanta,Meals,34\n", "Atlanta,Meals,22\n", "Atlanta,Meals,51\n", "\"\"\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "from StringIO import StringIO\n", "df = pd.read_csv(StringIO(transactions_text))" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "df.head()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "df.groupby(['trip']).head()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "df.groupby(['trip']).sum()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "df.groupby(['category']).sum()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "grid = df.groupby(['trip', 'category']\n", " ).sum().unstack('category').fillna(0)\n", "grid['Totals'] = df.groupby(['trip']).sum()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "grid" ], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }