Skip to content

Instantly share code, notes, and snippets.

@sergiks
Created February 21, 2018 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sergiks/0eb381fb59f7546a59e9fd66343008e4 to your computer and use it in GitHub Desktop.
Save sergiks/0eb381fb59f7546a59e9fd66343008e4 to your computer and use it in GitHub Desktop.
Filering rows with similar values within pandas DataSet
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Pandas play\n",
"Toster question https://toster.ru/q/507710"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import pandas as pd, numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"df = pd.DataFrame(\n",
" [\n",
" ['bar', 1, 'foo'],\n",
" ['foo', 999, 'bar'],\n",
" ['foo', 15, 'foo'],\n",
" ['smth', 4, 'foo'],\n",
" ['smth', 23, 'bar'],\n",
" ['foo', -19, 'foo'],\n",
" ['bar', 0, 'bar'],\n",
" ['smth', 88, 'bar'],\n",
" ['foo', 10500, 'foo'],\n",
" ['foo', 12, 'bar'],\n",
" ], columns=('co11', 'co12', 'co13')\n",
")\n"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style>\n",
" .dataframe thead tr:only-child th {\n",
" text-align: right;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: left;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>co11</th>\n",
" <th>co12</th>\n",
" <th>co13</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>bar</td>\n",
" <td>1</td>\n",
" <td>foo</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>foo</td>\n",
" <td>999</td>\n",
" <td>bar</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>foo</td>\n",
" <td>15</td>\n",
" <td>foo</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>smth</td>\n",
" <td>4</td>\n",
" <td>foo</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>smth</td>\n",
" <td>23</td>\n",
" <td>bar</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>foo</td>\n",
" <td>-19</td>\n",
" <td>foo</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>bar</td>\n",
" <td>0</td>\n",
" <td>bar</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>smth</td>\n",
" <td>88</td>\n",
" <td>bar</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>foo</td>\n",
" <td>10500</td>\n",
" <td>foo</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>foo</td>\n",
" <td>12</td>\n",
" <td>bar</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" co11 co12 co13\n",
"0 bar 1 foo\n",
"1 foo 999 bar\n",
"2 foo 15 foo\n",
"3 smth 4 foo\n",
"4 smth 23 bar\n",
"5 foo -19 foo\n",
"6 bar 0 bar\n",
"7 smth 88 bar\n",
"8 foo 10500 foo\n",
"9 foo 12 bar"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style>\n",
" .dataframe thead tr:only-child th {\n",
" text-align: right;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: left;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>co11</th>\n",
" <th>co12</th>\n",
" <th>co13</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>foo</td>\n",
" <td>15</td>\n",
" <td>foo</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>foo</td>\n",
" <td>-19</td>\n",
" <td>foo</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>bar</td>\n",
" <td>0</td>\n",
" <td>bar</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>foo</td>\n",
" <td>10500</td>\n",
" <td>foo</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" co11 co12 co13\n",
"2 foo 15 foo\n",
"5 foo -19 foo\n",
"6 bar 0 bar\n",
"8 foo 10500 foo"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df[df[['co11','co13']].nunique(axis=1) == 1]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"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.6.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment