Created
April 2, 2018 06:57
-
-
Save a-y-khan/9df8dac655846ef6a53c04b8a5b14431 to your computer and use it in GitHub Desktop.
Pandas DataFrame axis basics (Part 1)
This file contains 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": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Empty DataFrame:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0\n", | |
"2\n", | |
"Index([], dtype='object')\n", | |
"Index([], dtype='object')\n" | |
] | |
} | |
], | |
"source": [ | |
"import pandas as pd\n", | |
"\n", | |
"df = pd.DataFrame()\n", | |
"print(df.size)\n", | |
"print(len(df.axes))\n", | |
"print(df.axes[0])\n", | |
"print(df.axes[1])" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Initialize DataFrame with row data from array:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
" 0\n", | |
"0 A\n", | |
"1 B\n", | |
"RangeIndex(start=0, stop=2, step=1)\n", | |
"RangeIndex(start=0, stop=1, step=1)\n" | |
] | |
} | |
], | |
"source": [ | |
"df = pd.DataFrame(['A', 'B'])\n", | |
"print(df.head())\n", | |
"print(df.axes[0])\n", | |
"print(df.axes[1])" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Initialize DataFrame with column data from array:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
" 0 1\n", | |
"0 A B\n", | |
"RangeIndex(start=0, stop=1, step=1)\n", | |
"RangeIndex(start=0, stop=2, step=1)\n" | |
] | |
} | |
], | |
"source": [ | |
"df = pd.DataFrame(['A', 'B']).T\n", | |
"print(df.head())\n", | |
"print(df.axes[0])\n", | |
"print(df.axes[1])" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Initialize DataFrame with column data and labels:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
" A B\n", | |
"0 1 2\n", | |
"RangeIndex(start=0, stop=1, step=1)\n", | |
"Index(['A', 'B'], dtype='object')\n" | |
] | |
} | |
], | |
"source": [ | |
"df = pd.DataFrame([[1,2]], columns=['A', 'B'])\n", | |
"print(df.head())\n", | |
"print(df.axes[0])\n", | |
"print(df.axes[1])" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Initialize DataFrame with column data and labels, and index:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
" A B\n", | |
"C 1 2\n", | |
"D 3 4\n", | |
"Index(['C', 'D'], dtype='object')\n", | |
"Index(['A', 'B'], dtype='object')\n" | |
] | |
} | |
], | |
"source": [ | |
"df = pd.DataFrame([[1,2],[3,4]], index=list('CD'), columns=list('AB'))\n", | |
"print(df.head())\n", | |
"print(df.axes[0])\n", | |
"print(df.axes[1])" | |
] | |
}, | |
{ | |
"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