Skip to content

Instantly share code, notes, and snippets.

@greglandrum
Created April 29, 2021 09:49
Show Gist options
  • Save greglandrum/2ecee4e965872a8644237a7b95bb4759 to your computer and use it in GitHub Desktop.
Save greglandrum/2ecee4e965872a8644237a7b95bb4759 to your computer and use it in GitHub Desktop.
pandas append columns
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "import pandas as pd",
"execution_count": 1,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "df1 = pd.DataFrame({\"compound_id\":[100,102,107,120],\"data1\":[11,12,13,14],\"data2\":[21,22,23,24]})\ndf1",
"execution_count": 2,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 2,
"data": {
"text/plain": " compound_id data1 data2\n0 100 11 21\n1 102 12 22\n2 107 13 23\n3 120 14 24",
"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>compound_id</th>\n <th>data1</th>\n <th>data2</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>100</td>\n <td>11</td>\n <td>21</td>\n </tr>\n <tr>\n <th>1</th>\n <td>102</td>\n <td>12</td>\n <td>22</td>\n </tr>\n <tr>\n <th>2</th>\n <td>107</td>\n <td>13</td>\n <td>23</td>\n </tr>\n <tr>\n <th>3</th>\n <td>120</td>\n <td>14</td>\n <td>24</td>\n </tr>\n </tbody>\n</table>\n</div>"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "df2 = pd.DataFrame({\"descr1\":[101,102,103,104],\"descr2\":[201,202,203,204],\n \"descr3\":[301,302,303,304]})\ndf2",
"execution_count": 3,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 3,
"data": {
"text/plain": " descr1 descr2 descr3\n0 101 201 301\n1 102 202 302\n2 103 203 303\n3 104 204 304",
"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>descr1</th>\n <th>descr2</th>\n <th>descr3</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>101</td>\n <td>201</td>\n <td>301</td>\n </tr>\n <tr>\n <th>1</th>\n <td>102</td>\n <td>202</td>\n <td>302</td>\n </tr>\n <tr>\n <th>2</th>\n <td>103</td>\n <td>203</td>\n <td>303</td>\n </tr>\n <tr>\n <th>3</th>\n <td>104</td>\n <td>204</td>\n <td>304</td>\n </tr>\n </tbody>\n</table>\n</div>"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "df1.join(df2)",
"execution_count": 4,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 4,
"data": {
"text/plain": " compound_id data1 data2 descr1 descr2 descr3\n0 100 11 21 101 201 301\n1 102 12 22 102 202 302\n2 107 13 23 103 203 303\n3 120 14 24 104 204 304",
"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>compound_id</th>\n <th>data1</th>\n <th>data2</th>\n <th>descr1</th>\n <th>descr2</th>\n <th>descr3</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>100</td>\n <td>11</td>\n <td>21</td>\n <td>101</td>\n <td>201</td>\n <td>301</td>\n </tr>\n <tr>\n <th>1</th>\n <td>102</td>\n <td>12</td>\n <td>22</td>\n <td>102</td>\n <td>202</td>\n <td>302</td>\n </tr>\n <tr>\n <th>2</th>\n <td>107</td>\n <td>13</td>\n <td>23</td>\n <td>103</td>\n <td>203</td>\n <td>303</td>\n </tr>\n <tr>\n <th>3</th>\n <td>120</td>\n <td>14</td>\n <td>24</td>\n <td>104</td>\n <td>204</td>\n <td>304</td>\n </tr>\n </tbody>\n</table>\n</div>"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"toc": {
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"base_numbering": 1,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
},
"language_info": {
"name": "python",
"version": "3.9.1",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"gist": {
"id": "",
"data": {
"description": "pandas append columns",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment