Created
October 22, 2018 20:36
-
-
Save manangandhi7/194d1cbde86dee33fe7299c84a8ed439 to your computer and use it in GitHub Desktop.
pandas_outer_join_same_col
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": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import pandas as pd" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
" col1 col2 col3\n", | |
"0 1 2 3\n", | |
"1 2 4 5\n", | |
"2 3 4 5\n", | |
" col1 col2 col3\n", | |
"0 1 2 3\n", | |
"1 2 4 4\n", | |
"2 4 5 6\n" | |
] | |
} | |
], | |
"source": [ | |
"df1 = pd.DataFrame([[1,2,3], [2,4,5], [3,4,5]], columns = ['col1', 'col2', 'col3'])\n", | |
"df2 = pd.DataFrame([[1,2,3], [2,4,4], [4,5,6]], columns = ['col1', 'col2', 'col3'])\n", | |
"print(df1); print(df2)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"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>col1</th>\n", | |
" <th>col2</th>\n", | |
" <th>col3_x</th>\n", | |
" <th>col3_y</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>1</td>\n", | |
" <td>2</td>\n", | |
" <td>3.0</td>\n", | |
" <td>3.0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>2</td>\n", | |
" <td>4</td>\n", | |
" <td>5.0</td>\n", | |
" <td>4.0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>3</td>\n", | |
" <td>4</td>\n", | |
" <td>5.0</td>\n", | |
" <td>NaN</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>4</td>\n", | |
" <td>5</td>\n", | |
" <td>NaN</td>\n", | |
" <td>6.0</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" col1 col2 col3_x col3_y\n", | |
"0 1 2 3.0 3.0\n", | |
"1 2 4 5.0 4.0\n", | |
"2 3 4 5.0 NaN\n", | |
"3 4 5 NaN 6.0" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"pd.merge(df1, df2, how = 'outer', left_on = ['col1', 'col2'], right_on = ['col1', 'col2'])\n", | |
"#how = {'left', 'right', 'outer', 'inner'}, default 'inner'" | |
] | |
} | |
], | |
"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.6" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment