{ "metadata": { "name": "", "signature": "sha256:7413be9ef690be14adc44dd2a9463fcf2856300e523a461a701de01478a96063" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Delete Duplicates In Pandas\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": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create dataframe with duplicates" ] }, { "cell_type": "code", "collapsed": false, "input": [ "raw_data = {'first_name': ['Jason', 'Jason', 'Tina', 'Jake', 'Amy'], \n", " 'last_name': ['Miller', 'Miller', 'Ali', 'Milner', 'Cooze'], \n", " 'age': [42, 42, 36, 24, 73], \n", " 'preTestScore': [4, 4, 31, 2, 3],\n", " 'postTestScore': [25, 25, 57, 62, 70]}\n", "df = pd.DataFrame(raw_data, columns = ['first_name', 'last_name', 'age', 'preTestScore', 'postTestScore'])\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", " \n", " \n", " \n", " \n", " \n", " \n", "
first_namelast_nameagepreTestScorepostTestScore
0 Jason Miller 42 4 25
1 Jason Miller 42 4 25
2 Tina Ali 36 31 57
3 Jake Milner 24 2 62
4 Amy Cooze 73 3 70
\n", "

5 rows \u00d7 5 columns

\n", "
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 5, "text": [ " first_name last_name age preTestScore postTestScore\n", "0 Jason Miller 42 4 25\n", "1 Jason Miller 42 4 25\n", "2 Tina Ali 36 31 57\n", "3 Jake Milner 24 2 62\n", "4 Amy Cooze 73 3 70\n", "\n", "[5 rows x 5 columns]" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Identify which observations are duplicates" ] }, { "cell_type": "code", "collapsed": false, "input": [ "df.duplicated()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 10, "text": [ "0 False\n", "1 True\n", "2 False\n", "3 False\n", "4 False\n", "dtype: bool" ] } ], "prompt_number": 10 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Drop duplicates" ] }, { "cell_type": "code", "collapsed": false, "input": [ "df.drop_duplicates()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Drop duplicates in the first name column, but take the last obs in the duplicated set" ] }, { "cell_type": "code", "collapsed": false, "input": [ "df.drop_duplicates(['first_name'], take_last=True)" ], "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", "
first_namelast_nameagepreTestScorepostTestScore
1 Jason Miller 42 4 25
2 Tina Ali 36 31 57
3 Jake Milner 24 2 62
4 Amy Cooze 73 3 70
\n", "

4 rows \u00d7 5 columns

\n", "
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 9, "text": [ " first_name last_name age preTestScore postTestScore\n", "1 Jason Miller 42 4 25\n", "2 Tina Ali 36 31 57\n", "3 Jake Milner 24 2 62\n", "4 Amy Cooze 73 3 70\n", "\n", "[4 rows x 5 columns]" ] } ], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }