{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import pprint\n", "import json" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'1.3.2'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.__version__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## create from lists" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/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", "
namesages
0john33
1mary22
2peter45
3gary23
4anne12
\n", "
" ], "text/plain": [ " names ages\n", "0 john 33\n", "1 mary 22\n", "2 peter 45\n", "3 gary 23\n", "4 anne 12" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "names = ['john','mary','peter','gary','anne']\n", "ages = [33,22,45,23,12]\n", "\n", "df = pd.DataFrame({\n", " 'names':names,\n", " 'ages':ages\n", "})\n", "df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## create from list of dicts" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/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", "
namegenderage
0johnmale45
1maryfemale19
2petermale34
\n", "
" ], "text/plain": [ " name gender age\n", "0 john male 45\n", "1 mary female 19\n", "2 peter male 34" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data_dicts = [\n", " {'name':\"john\",\"gender\":'male','age':45},\n", " {'name':\"mary\", 'gender':\"female\",'age':19},\n", " {'name':\"peter\",'gender':'male', 'age':34}\n", "]\n", "\n", "# must reassign since the append method does not work in place\n", "df = pd.DataFrame.from_records(data_dicts)\n", "df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## create from single dict use keys as index" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " \"alice\": 12,\n", " \"bob\": 20,\n", " \"charlie\": 33\n", "}\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
age
alice12
bob20
charlie33
\n", "
" ], "text/plain": [ " age\n", "alice 12\n", "bob 20\n", "charlie 33" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d = {\"alice\": 12, \"bob\": 20, \"charlie\": 33}\n", "\n", "print(json.dumps(d, indent=2))\n", "\n", "pd.DataFrame.from_dict(d, orient='index', columns=['age'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## create an empty dataframe and append rows" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/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", "
col_acol_b
05.010.0
11.0100.0
232.0999.0
\n", "
" ], "text/plain": [ " col_a col_b\n", "0 5.0 10.0\n", "1 1.0 100.0\n", "2 32.0 999.0" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.DataFrame()\n", "\n", "# must reassign since the append method does not work in place\n", "df = df.append({'col_a':5,'col_b':10}, ignore_index=True)\n", "df = df.append({'col_a':1,'col_b':100}, ignore_index=True)\n", "df = df.append({'col_a':32,'col_b':999}, ignore_index=True)\n", "\n", "df" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## crate dataframe with specific types" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/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", "
namegenderage
0johnmale45
1maryfemale19
2petermale34
\n", "
" ], "text/plain": [ " name gender age\n", "0 john male 45\n", "1 mary female 19\n", "2 peter male 34" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data_dicts = [\n", " {'name':\"john\",\"gender\":'male','age':45},\n", " {'name':\"mary\", 'gender':\"female\",'age':19},\n", " {'name':\"peter\",'gender':'male', 'age':34}\n", "]\n", "\n", "# must reassign since the append method does not work in place\n", "df = pd.DataFrame.from_records(data_dicts,)\n", "df" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.8.10" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }