Skip to content
Snippets Groups Projects
Introduction-to-Pandas--master.ipynb 673 KiB
Newer Older
Andreas Herten's avatar
Andreas Herten committed
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>entries</th>\n",
       "      <td>1.2</td>\n",
       "      <td>2018-02-26</td>\n",
Andreas Herten's avatar
Andreas Herten committed
       "      <td>0.986231</td>\n",
       "      <td>Same</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>entries</th>\n",
       "      <td>1.2</td>\n",
       "      <td>2018-02-26</td>\n",
Andreas Herten's avatar
Andreas Herten committed
       "      <td>-0.718282</td>\n",
       "      <td>Same</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           A          B         C     E\n",
       "D                                      \n",
       "entries  1.2 2018-02-26  0.986231  Same\n",
       "entries  1.2 2018-02-26 -0.718282  Same"
     "execution_count": 40,
Andreas Herten's avatar
Andreas Herten committed
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_demo_indexed.loc[\"entries\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Advanced Slicing: Logical Slicing\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
Andreas Herten's avatar
Andreas Herten committed
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.2</td>\n",
       "      <td>2018-02-26</td>\n",
       "      <td>1.718282</td>\n",
       "      <td>column</td>\n",
       "      <td>Same</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.2</td>\n",
       "      <td>2018-02-26</td>\n",
       "      <td>0.986231</td>\n",
       "      <td>entries</td>\n",
       "      <td>Same</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     A          B         C        D     E\n",
       "1  1.2 2018-02-26  1.718282   column  Same\n",
       "3  1.2 2018-02-26  0.986231  entries  Same"
     "execution_count": 41,
Andreas Herten's avatar
Andreas Herten committed
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_demo[df_demo[\"C\"] > 0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
Andreas Herten's avatar
Andreas Herten committed
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1.2</td>\n",
       "      <td>2018-02-26</td>\n",
       "      <td>-0.718282</td>\n",
       "      <td>entries</td>\n",
       "      <td>Same</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     A          B         C        D     E\n",
       "4  1.2 2018-02-26 -0.718282  entries  Same"
     "execution_count": 42,
Andreas Herten's avatar
Andreas Herten committed
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_demo[(df_demo[\"C\"] < 0) & (df_demo[\"D\"] == \"entries\")]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Adding to Existing Data Frame\n",
    "\n",
    "* Add new columns with `frame[\"new col\"] = something` or `.insert()`\n",
    "* Add new rows with `frame.append()`\n",
    "* Combine data frames\n",
    "    - Concat: Combine several data frames along an axis\n",
    "    - Merge: Combine data frames on basis of common columns; database-style\n",
    "    - (Join)\n",
    "    - See user guide [on merging](https://pandas.pydata.org/pandas-docs/stable/user_guide/merging.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
Andreas Herten's avatar
Andreas Herten committed
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.2</td>\n",
       "      <td>2018-02-26</td>\n",
       "      <td>-2.718282</td>\n",
       "      <td>This</td>\n",
       "      <td>Same</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.2</td>\n",
       "      <td>2018-02-26</td>\n",
       "      <td>1.718282</td>\n",
       "      <td>column</td>\n",
       "      <td>Same</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.2</td>\n",
       "      <td>2018-02-26</td>\n",
       "      <td>-1.304068</td>\n",
       "      <td>has</td>\n",
       "      <td>Same</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     A          B         C       D     E\n",
       "0  1.2 2018-02-26 -2.718282    This  Same\n",
       "1  1.2 2018-02-26  1.718282  column  Same\n",
       "2  1.2 2018-02-26 -1.304068     has  Same"
      ]
     },
     "execution_count": 43,
Andreas Herten's avatar
Andreas Herten committed
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_demo.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
Andreas Herten's avatar
Andreas Herten committed
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "      <th>F</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.2</td>\n",
       "      <td>2018-02-26</td>\n",
       "      <td>-2.718282</td>\n",
       "      <td>This</td>\n",
       "      <td>Same</td>\n",
       "      <td>-3.918282</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.2</td>\n",
       "      <td>2018-02-26</td>\n",
       "      <td>1.718282</td>\n",
       "      <td>column</td>\n",
       "      <td>Same</td>\n",
       "      <td>0.518282</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.2</td>\n",
       "      <td>2018-02-26</td>\n",
       "      <td>-1.304068</td>\n",
       "      <td>has</td>\n",
       "      <td>Same</td>\n",
       "      <td>-2.504068</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     A          B         C       D     E         F\n",
       "0  1.2 2018-02-26 -2.718282    This  Same -3.918282\n",
       "1  1.2 2018-02-26  1.718282  column  Same  0.518282\n",
       "2  1.2 2018-02-26 -1.304068     has  Same -2.504068"
      ]
     },
     "execution_count": 44,
Andreas Herten's avatar
Andreas Herten committed
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_demo[\"F\"] = df_demo[\"C\"] - df_demo[\"A\"]\n",
    "df_demo.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
Andreas Herten's avatar
Andreas Herten committed
   "metadata": {},
   "outputs": [],
   "source": [
    "df_demo.insert(len(df_demo) + 1, \"G\", df_demo[\"C\"] ** 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
Andreas Herten's avatar
Andreas Herten committed
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "      <th>F</th>\n",
       "      <th>G</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.2</td>\n",
       "      <td>2018-02-26</td>\n",
       "      <td>-1.304068</td>\n",
       "      <td>has</td>\n",
       "      <td>Same</td>\n",
       "      <td>-2.504068</td>\n",
       "      <td>1.700594</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.2</td>\n",
       "      <td>2018-02-26</td>\n",
       "      <td>0.986231</td>\n",
       "      <td>entries</td>\n",
       "      <td>Same</td>\n",
       "      <td>-0.213769</td>\n",
       "      <td>0.972652</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1.2</td>\n",
       "      <td>2018-02-26</td>\n",
       "      <td>-0.718282</td>\n",
       "      <td>entries</td>\n",
       "      <td>Same</td>\n",
       "      <td>-1.918282</td>\n",
       "      <td>0.515929</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     A          B         C        D     E         F         G\n",
       "2  1.2 2018-02-26 -1.304068      has  Same -2.504068  1.700594\n",
       "3  1.2 2018-02-26  0.986231  entries  Same -0.213769  0.972652\n",
       "4  1.2 2018-02-26 -0.718282  entries  Same -1.918282  0.515929"
      ]
     },
     "execution_count": 46,
Andreas Herten's avatar
Andreas Herten committed
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_demo.tail(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
Andreas Herten's avatar
Andreas Herten committed
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "      <th>F</th>\n",
       "      <th>G</th>\n",
Andreas Herten's avatar
Andreas Herten committed
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.2</td>\n",
       "      <td>2018-02-26</td>\n",
       "      <td>-2.718282</td>\n",
       "      <td>This</td>\n",
       "      <td>Same</td>\n",
       "      <td>-3.918282</td>\n",
       "      <td>7.389056</td>\n",
Andreas Herten's avatar
Andreas Herten committed
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.2</td>\n",
       "      <td>2018-02-26</td>\n",
       "      <td>1.718282</td>\n",
       "      <td>column</td>\n",
       "      <td>Same</td>\n",
       "      <td>0.518282</td>\n",
       "      <td>2.952492</td>\n",
Andreas Herten's avatar
Andreas Herten committed
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.2</td>\n",
       "      <td>2018-02-26</td>\n",
       "      <td>-1.304068</td>\n",
       "      <td>has</td>\n",
       "      <td>Same</td>\n",
       "      <td>-2.504068</td>\n",
       "      <td>1.700594</td>\n",
Andreas Herten's avatar
Andreas Herten committed
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.2</td>\n",
       "      <td>2018-02-26</td>\n",
       "      <td>0.986231</td>\n",
       "      <td>entries</td>\n",
       "      <td>Same</td>\n",
       "      <td>-0.213769</td>\n",
       "      <td>0.972652</td>\n",
Andreas Herten's avatar
Andreas Herten committed
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1.2</td>\n",
       "      <td>2018-02-26</td>\n",
       "      <td>-0.718282</td>\n",
       "      <td>entries</td>\n",
       "      <td>Same</td>\n",
       "      <td>-1.918282</td>\n",
       "      <td>0.515929</td>\n",
Andreas Herten's avatar
Andreas Herten committed
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1.3</td>\n",
       "      <td>2018-02-27</td>\n",
       "      <td>-0.777000</td>\n",
       "      <td>has it?</td>\n",
       "      <td>Same</td>\n",
       "      <td>23.000000</td>\n",
       "      <td>NaN</td>\n",
Andreas Herten's avatar
Andreas Herten committed
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     A          B         C        D     E          F         G\n",
       "0  1.2 2018-02-26 -2.718282     This  Same  -3.918282  7.389056\n",
       "1  1.2 2018-02-26  1.718282   column  Same   0.518282  2.952492\n",
       "2  1.2 2018-02-26 -1.304068      has  Same  -2.504068  1.700594\n",
       "3  1.2 2018-02-26  0.986231  entries  Same  -0.213769  0.972652\n",
       "4  1.2 2018-02-26 -0.718282  entries  Same  -1.918282  0.515929\n",
       "5  1.3 2018-02-27 -0.777000  has it?  Same  23.000000       NaN"
     "execution_count": 47,
Andreas Herten's avatar
Andreas Herten committed
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_demo.append(\n",
    "    {\"A\": 1.3, \"B\": pd.Timestamp(\"2018-02-27\"), \"C\": -0.777, \"D\": \"has it?\", \"E\": \"Same\", \"F\": 23},\n",
    "    ignore_index=True\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Combining Frames\n",
    "\n",
    "* First, create some simpler data frame to show `.concat()` and `.merge()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
Andreas Herten's avatar
Andreas Herten committed
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Key</th>\n",
       "      <th>Value</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>First</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Second</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      Key  Value\n",
       "0   First      1\n",
       "1  Second      1"
      ]
     },
     "execution_count": 48,
Andreas Herten's avatar
Andreas Herten committed
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_1 = pd.DataFrame({\"Key\": [\"First\", \"Second\"], \"Value\": [1, 1]})\n",
    "df_1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
Andreas Herten's avatar
Andreas Herten committed
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Key</th>\n",
       "      <th>Value</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>First</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Second</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      Key  Value\n",
       "0   First      2\n",
       "1  Second      2"
      ]
     },
     "execution_count": 49,
Andreas Herten's avatar
Andreas Herten committed
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_2 = pd.DataFrame({\"Key\": [\"First\", \"Second\"], \"Value\": [2, 2]})\n",
    "df_2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "* Concatenate list of data frame vertically (`axis=0`)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
Andreas Herten's avatar
Andreas Herten committed
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Key</th>\n",
       "      <th>Value</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>First</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Second</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>First</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Second</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      Key  Value\n",
       "0   First      1\n",
       "1  Second      1\n",
       "0   First      2\n",
       "1  Second      2"
      ]
     },
     "execution_count": 50,
Andreas Herten's avatar
Andreas Herten committed
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.concat([df_1, df_2])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "* Same, but re-index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
Andreas Herten's avatar
Andreas Herten committed
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Key</th>\n",
       "      <th>Value</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>First</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Second</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>First</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Second</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      Key  Value\n",
       "0   First      1\n",
       "1  Second      1\n",
       "2   First      2\n",
       "3  Second      2"
      ]
     },
     "execution_count": 51,
Andreas Herten's avatar
Andreas Herten committed
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.concat([df_1, df_2], ignore_index=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "* Concat, but horizontally"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
Andreas Herten's avatar
Andreas Herten committed
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Key</th>\n",
       "      <th>Value</th>\n",
       "      <th>Key</th>\n",
       "      <th>Value</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>First</td>\n",
       "      <td>1</td>\n",
       "      <td>First</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Second</td>\n",
       "      <td>1</td>\n",
       "      <td>Second</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      Key  Value     Key  Value\n",
       "0   First      1   First      2\n",
       "1  Second      1  Second      2"
      ]
     },
     "execution_count": 52,
Andreas Herten's avatar
Andreas Herten committed
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.concat([df_1, df_2], axis=1)"