Last active
April 13, 2018 07:08
-
-
Save a-y-khan/2ac5987bc2e869df16bfdb686a48fc6c to your computer and use it in GitHub Desktop.
Pandas DataFrame axis basics (Part 2)
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": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
" A B\n", | |
"0 1 2\n", | |
"1 3 4\n", | |
"2 5 6\n", | |
"3 7 8\n" | |
] | |
} | |
], | |
"source": [ | |
"import pandas as pd\n", | |
"\n", | |
"df = pd.DataFrame([[1,2],[3,4],[5,6],[7,8]], columns=list('AB'))\n", | |
"print(df.head())" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Location based indexing\n", | |
"\n", | |
"#### Use of loc and iloc produces equivalent results here:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"A 3\n", | |
"B 4\n", | |
"Name: 1, dtype: int64\n", | |
"A 3\n", | |
"B 4\n", | |
"Name: 1, dtype: int64\n" | |
] | |
} | |
], | |
"source": [ | |
"print(df.loc[1])\n", | |
"print(df.iloc[1])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
" A B\n", | |
"C 1 2\n", | |
"D 3 4\n", | |
"E 5 6\n", | |
"F 7 8\n" | |
] | |
} | |
], | |
"source": [ | |
"df = pd.DataFrame([[1,2],[3,4],[5,6],[7,8]], index=list('CDEF'), columns=list('AB'))\n", | |
"print(df.head())" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Location based indexing" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"A 7\n", | |
"B 8\n", | |
"Name: F, dtype: int64\n", | |
"A 7\n", | |
"B 8\n", | |
"Name: F, dtype: int64\n" | |
] | |
} | |
], | |
"source": [ | |
"print(df.loc['F'])\n", | |
"print(df.iloc[3])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"7\n", | |
"7\n" | |
] | |
} | |
], | |
"source": [ | |
"print(df.loc['F', 'A'])\n", | |
"print(df.iloc[3, 0])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"C 1\n", | |
"D 3\n", | |
"E 5\n", | |
"F 7\n", | |
"Name: A, dtype: int64\n", | |
"C 1\n", | |
"D 3\n", | |
"E 5\n", | |
"F 7\n", | |
"Name: A, dtype: int64\n" | |
] | |
} | |
], | |
"source": [ | |
"print(df['A'])\n", | |
"print(df.A)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"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.4" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment