Created
May 14, 2018 04:03
-
-
Save Adhira-Deogade/a4961364395764cce4c24f48283b9290 to your computer and use it in GitHub Desktop.
my gist
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"import pandas as pd\n", | |
"\n", | |
"from pandas import Series, DataFrame" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Selecting and retrieving data\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"row 1 0\n", | |
"row 2 1\n", | |
"row 3 2\n", | |
"row 4 3\n", | |
"row 5 4\n", | |
"row 6 5\n", | |
"row 7 6\n", | |
"row 8 7\n", | |
"dtype: int64" | |
] | |
}, | |
"execution_count": 2, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"series_obj = Series(np.arange(8),index=['row 1','row 2','row 3','row 4','row 5','row 6','row 7','row 8'])\n", | |
"series_obj" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"6" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"series_obj['row 7']" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"row 1 0\n", | |
"row 8 7\n", | |
"dtype: int64" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"series_obj[[0,7]]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"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>column 1</th>\n", | |
" <th>column 2</th>\n", | |
" <th>column 3</th>\n", | |
" <th>column 4</th>\n", | |
" <th>column 5</th>\n", | |
" <th>column 6</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>row 1</th>\n", | |
" <td>0.870124</td>\n", | |
" <td>0.582277</td>\n", | |
" <td>0.278839</td>\n", | |
" <td>0.185911</td>\n", | |
" <td>0.411100</td>\n", | |
" <td>0.117376</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>row 2</th>\n", | |
" <td>0.684969</td>\n", | |
" <td>0.437611</td>\n", | |
" <td>0.556229</td>\n", | |
" <td>0.367080</td>\n", | |
" <td>0.402366</td>\n", | |
" <td>0.113041</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>row 3</th>\n", | |
" <td>0.447031</td>\n", | |
" <td>0.585445</td>\n", | |
" <td>0.161985</td>\n", | |
" <td>0.520719</td>\n", | |
" <td>0.326051</td>\n", | |
" <td>0.699186</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>row 4</th>\n", | |
" <td>0.366395</td>\n", | |
" <td>0.836375</td>\n", | |
" <td>0.481343</td>\n", | |
" <td>0.516502</td>\n", | |
" <td>0.383048</td>\n", | |
" <td>0.997541</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>row 5</th>\n", | |
" <td>0.514244</td>\n", | |
" <td>0.559053</td>\n", | |
" <td>0.034450</td>\n", | |
" <td>0.719930</td>\n", | |
" <td>0.421004</td>\n", | |
" <td>0.436935</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>row 6</th>\n", | |
" <td>0.281701</td>\n", | |
" <td>0.900274</td>\n", | |
" <td>0.669612</td>\n", | |
" <td>0.456069</td>\n", | |
" <td>0.289804</td>\n", | |
" <td>0.525819</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" column 1 column 2 column 3 column 4 column 5 column 6\n", | |
"row 1 0.870124 0.582277 0.278839 0.185911 0.411100 0.117376\n", | |
"row 2 0.684969 0.437611 0.556229 0.367080 0.402366 0.113041\n", | |
"row 3 0.447031 0.585445 0.161985 0.520719 0.326051 0.699186\n", | |
"row 4 0.366395 0.836375 0.481343 0.516502 0.383048 0.997541\n", | |
"row 5 0.514244 0.559053 0.034450 0.719930 0.421004 0.436935\n", | |
"row 6 0.281701 0.900274 0.669612 0.456069 0.289804 0.525819" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"np.random.seed(25)\n", | |
"Df_obj = DataFrame(np.random.rand(36).reshape((6,6)), index=['row 1','row 2','row 3','row 4','row 5','row 6'],\n", | |
" columns = ['column 1','column 2','column 3','column 4','column 5','column 6'])\n", | |
"Df_obj" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"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>column 5</th>\n", | |
" <th>column 2</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>row 2</th>\n", | |
" <td>0.402366</td>\n", | |
" <td>0.437611</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>row 5</th>\n", | |
" <td>0.421004</td>\n", | |
" <td>0.559053</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" column 5 column 2\n", | |
"row 2 0.402366 0.437611\n", | |
"row 5 0.421004 0.559053" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"Df_obj.loc[['row 2', 'row 5'], ['column 5', 'column 2']]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Data Slicing\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"row 2 1\n", | |
"row 3 2\n", | |
"row 4 3\n", | |
"row 5 4\n", | |
"row 6 5\n", | |
"row 7 6\n", | |
"dtype: int64" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"series_obj['row 2' : 'row 7']\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Data comparison" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"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>column 1</th>\n", | |
" <th>column 2</th>\n", | |
" <th>column 3</th>\n", | |
" <th>column 4</th>\n", | |
" <th>column 5</th>\n", | |
" <th>column 6</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>row 1</th>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>True</td>\n", | |
" <td>False</td>\n", | |
" <td>True</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>row 2</th>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>True</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>row 3</th>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>True</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>row 4</th>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>row 5</th>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>True</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>row 6</th>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" column 1 column 2 column 3 column 4 column 5 column 6\n", | |
"row 1 False False False True False True\n", | |
"row 2 False False False False False True\n", | |
"row 3 False False True False False False\n", | |
"row 4 False False False False False False\n", | |
"row 5 False False True False False False\n", | |
"row 6 False False False False False False" | |
] | |
}, | |
"execution_count": 10, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"Df_obj< 0.2" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Filtering with scalars" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 38, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"row 1 0\n", | |
"row 2 1\n", | |
"row 3 2\n", | |
"row 4 3\n", | |
"row 5 4\n", | |
"row 6 5\n", | |
"dtype: int64" | |
] | |
}, | |
"execution_count": 38, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"series_obj[series_obj<6]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Setting values with scalars" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 39, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"series_obj['row 1', 'row 2', 'row 3'] = 8" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 40, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"row 1 8\n", | |
"row 2 8\n", | |
"row 3 8\n", | |
"row 4 3\n", | |
"row 5 4\n", | |
"row 6 5\n", | |
"row 7 6\n", | |
"row 8 7\n", | |
"dtype: int64" | |
] | |
}, | |
"execution_count": 40, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"series_obj" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 2", | |
"language": "python", | |
"name": "python2" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.13" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment