{ "metadata": { "name": "", "signature": "sha256:071ef819ee4ee2f5f208b385c7b0847ca2d00580f592f29da459df147b63cb95" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Sorting Rows Of pandas Dataframes\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 pandas as pd" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 14 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create dataframe" ] }, { "cell_type": "code", "collapsed": false, "input": [ "data = {'name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'], \n", " 'year': [2012, 2012, 2013, 2014, 2014], \n", " 'reports': [4, 24, 31, 2, 3],\n", " 'coverage': [25, 94, 57, 62, 70]}\n", "df = pd.DataFrame(data, index = ['Cochice', 'Pima', 'Santa Cruz', 'Maricopa', 'Yuma'])\n", "df" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
coveragenamereportsyear
Cochice 25 Jason 4 2012
Pima 94 Molly 24 2012
Santa Cruz 57 Tina 31 2013
Maricopa 62 Jake 2 2014
Yuma 70 Amy 3 2014
\n", "

5 rows \u00d7 4 columns

\n", "
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 3, "text": [ " coverage name reports year\n", "Cochice 25 Jason 4 2012\n", "Pima 94 Molly 24 2012\n", "Santa Cruz 57 Tina 31 2013\n", "Maricopa 62 Jake 2 2014\n", "Yuma 70 Amy 3 2014\n", "\n", "[5 rows x 4 columns]" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Sort the dataframe's rows by reports, in descending order" ] }, { "cell_type": "code", "collapsed": false, "input": [ "df.sort_index(by='reports', ascending=0)" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
coveragenamereportsyear
Santa Cruz 57 Tina 31 2013
Pima 94 Molly 24 2012
Cochice 25 Jason 4 2012
Yuma 70 Amy 3 2014
Maricopa 62 Jake 2 2014
\n", "

5 rows \u00d7 4 columns

\n", "
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 15, "text": [ " coverage name reports year\n", "Santa Cruz 57 Tina 31 2013\n", "Pima 94 Molly 24 2012\n", "Cochice 25 Jason 4 2012\n", "Yuma 70 Amy 3 2014\n", "Maricopa 62 Jake 2 2014\n", "\n", "[5 rows x 4 columns]" ] } ], "prompt_number": 15 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Sort the dataframe's rows by coverage and then by reports, in ascending order" ] }, { "cell_type": "code", "collapsed": false, "input": [ "df.sort_index(by=['coverage', 'reports'])" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
coveragenamereportsyear
Cochice 25 Jason 4 2012
Santa Cruz 57 Tina 31 2013
Maricopa 62 Jake 2 2014
Yuma 70 Amy 3 2014
Pima 94 Molly 24 2012
\n", "

5 rows \u00d7 4 columns

\n", "
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 10, "text": [ " coverage name reports year\n", "Cochice 25 Jason 4 2012\n", "Santa Cruz 57 Tina 31 2013\n", "Maricopa 62 Jake 2 2014\n", "Yuma 70 Amy 3 2014\n", "Pima 94 Molly 24 2012\n", "\n", "[5 rows x 4 columns]" ] } ], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }