{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import pandas as pd" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n" } }, "source": [ "# concat" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "s1 = pd.Series([\"red\", \"black\"], name=\"color\")\n", "s2 = pd.Series([\"white\", \"gray\"], name=\"colorname\")\n", "df1 = pd.DataFrame(\n", " {\n", " \"color\": [\"red\", \"orange\", \"blue\", \"green\"],\n", " }\n", ")\n", "df2 = pd.DataFrame(\n", " {\n", " \"color\": [\"black\", \"white\", \"yellow\", \"purple\"],\n", " \"shape\": [\"circle\", \"square\", \"triangle\", \"star\"],\n", " }\n", ")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } }, "outputs": [ { "data": { "text/plain": [ "0 red\n", "1 black\n", "0 white\n", "1 gray\n", "dtype: object" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "pd.concat([s1, s2])" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } }, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
colorshape
0redNaN
1orangeNaN
2blueNaN
3greenNaN
0blackcircle
1whitesquare
2yellowtriangle
3purplestar
\n", "
" ], "text/plain": [ " color shape\n", "0 red NaN\n", "1 orange NaN\n", "2 blue NaN\n", "3 green NaN\n", "0 black circle\n", "1 white square\n", "2 yellow triangle\n", "3 purple star" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.concat([df1, df2])" ] }, { "cell_type": "code", "execution_count": 5, "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", "
colorshape
1orangeNaN
1whitesquare
3greenNaN
3purplestar
\n", "
" ], "text/plain": [ " color shape\n", "1 orange NaN\n", "1 white square\n", "3 green NaN\n", "3 purple star" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.concat([df1, df2]).loc[[1,3], :]" ] }, { "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", "
colorshape
1orangeNaN
3greenNaN
\n", "
" ], "text/plain": [ " color shape\n", "1 orange NaN\n", "3 green NaN" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.concat([df1, df2]).iloc[[1,3], :]" ] }, { "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", "
colorshape
1orangeNaN
3greenNaN
\n", "
" ], "text/plain": [ " color shape\n", "1 orange NaN\n", "3 green NaN" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.concat([df1, df2], ignore_index=True).loc[[1,3], :]" ] }, { "cell_type": "code", "execution_count": 8, "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", "
colorshape
1orangeNaN
3greenNaN
\n", "
" ], "text/plain": [ " color shape\n", "1 orange NaN\n", "3 green NaN" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.concat([df1, df2], ignore_index=True).iloc[[1,3], :]" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } }, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
colorshape
df10redNaN
1orangeNaN
2blueNaN
3greenNaN
df20blackcircle
1whitesquare
2yellowtriangle
3purplestar
\n", "
" ], "text/plain": [ " color shape\n", "df1 0 red NaN\n", " 1 orange NaN\n", " 2 blue NaN\n", " 3 green NaN\n", "df2 0 black circle\n", " 1 white square\n", " 2 yellow triangle\n", " 3 purple star" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.concat([df1, df2], keys=[\"df1\", \"df2\"])" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } }, "outputs": [ { "data": { "text/plain": [ "MultiIndex([('df1', 0),\n", " ('df1', 1),\n", " ('df1', 2),\n", " ('df1', 3),\n", " ('df2', 0),\n", " ('df2', 1),\n", " ('df2', 2),\n", " ('df2', 3)],\n", " )" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.concat([df1, df2], keys=[\"df1\", \"df2\"]).index" ] }, { "cell_type": "code", "execution_count": 10, "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", "
colorshape
df11orangeNaN
3greenNaN
\n", "
" ], "text/plain": [ " color shape\n", "df1 1 orange NaN\n", " 3 green NaN" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.concat([df1, df2], keys=[\"df1\", \"df2\"]).loc[('df1', [1,3]), :]" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } }, "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", " \n", " \n", " \n", " \n", " \n", "
color0
0redNaN
1orangeNaN
2blueNaN
3greenNaN
0NaNwhite
1NaNgray
\n", "
" ], "text/plain": [ " color 0\n", "0 red NaN\n", "1 orange NaN\n", "2 blue NaN\n", "3 green NaN\n", "0 NaN white\n", "1 NaN gray" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.concat([df1, s2]) # default axis=0" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } }, "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", "
colorcolorname
0redwhite
1orangegray
2blueNaN
3greenNaN
\n", "
" ], "text/plain": [ " color colorname\n", "0 red white\n", "1 orange gray\n", "2 blue NaN\n", "3 green NaN" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.concat([df1, s2], axis=1)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } }, "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", "
colorcolorname
0redwhite
1orangegray
2blueNaN
3greenNaN
\n", "
" ], "text/plain": [ " color colorname\n", "0 red white\n", "1 orange gray\n", "2 blue NaN\n", "3 green NaN" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.concat([df1, s2], axis=\"columns\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3.10.4 ('sspai-100-hours-series-python')", "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.10.4" }, "vscode": { "interpreter": { "hash": "7a101baf08afe636412f97dd4a9fc2e65b6f84f0ec50413bf3e19b04a26b8ba6" } } }, "nbformat": 4, "nbformat_minor": 0 }