Skip to content

Instantly share code, notes, and snippets.

@Erlemar
Created June 23, 2017 11:58
Show Gist options
  • Save Erlemar/4df89294dfdb11fc103c0bb59c77e962 to your computer and use it in GitHub Desktop.
Save Erlemar/4df89294dfdb11fc103c0bb59c77e962 to your computer and use it in GitHub Desktop.
Merging two dataframes
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"df1 = pd.DataFrame({'IPv4 Address': ['69.89.31.226', '61.89.31.226', '79.81.31.226', '69.81.31.225'],\n",
" 'some_column': [1,2,3,4], 'abother_column': ['q', 'f', 'g', 'h']})\n",
"df2 = pd.DataFrame({'IPv4 Address': ['69.89.31.226', '61.89.37.226', '79.81.31.226', '69.81.31.236', '69.81.31.222'],\n",
" 'some_column1': [11,21,31,41, 51], 'abother_column1': ['qq', 'f1', 'g1', 'h1', 'j1']})"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style>\n",
" .dataframe thead tr:only-child th {\n",
" text-align: right;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: left;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>IPv4 Address</th>\n",
" <th>abother_column</th>\n",
" <th>some_column</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>69.89.31.226</td>\n",
" <td>q</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>61.89.31.226</td>\n",
" <td>f</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>79.81.31.226</td>\n",
" <td>g</td>\n",
" <td>3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>69.81.31.225</td>\n",
" <td>h</td>\n",
" <td>4</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" IPv4 Address abother_column some_column\n",
"0 69.89.31.226 q 1\n",
"1 61.89.31.226 f 2\n",
"2 79.81.31.226 g 3\n",
"3 69.81.31.225 h 4"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df1"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style>\n",
" .dataframe thead tr:only-child th {\n",
" text-align: right;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: left;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>IPv4 Address</th>\n",
" <th>abother_column1</th>\n",
" <th>some_column1</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>69.89.31.226</td>\n",
" <td>qq</td>\n",
" <td>11</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>61.89.37.226</td>\n",
" <td>f1</td>\n",
" <td>21</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>79.81.31.226</td>\n",
" <td>g1</td>\n",
" <td>31</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>69.81.31.236</td>\n",
" <td>h1</td>\n",
" <td>41</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>69.81.31.222</td>\n",
" <td>j1</td>\n",
" <td>51</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" IPv4 Address abother_column1 some_column1\n",
"0 69.89.31.226 qq 11\n",
"1 61.89.37.226 f1 21\n",
"2 79.81.31.226 g1 31\n",
"3 69.81.31.236 h1 41\n",
"4 69.81.31.222 j1 51"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df2"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style>\n",
" .dataframe thead tr:only-child th {\n",
" text-align: right;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: left;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>IPv4 Address</th>\n",
" <th>abother_column</th>\n",
" <th>some_column</th>\n",
" <th>abother_column1</th>\n",
" <th>some_column1</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>69.89.31.226</td>\n",
" <td>q</td>\n",
" <td>1</td>\n",
" <td>qq</td>\n",
" <td>11</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>79.81.31.226</td>\n",
" <td>g</td>\n",
" <td>3</td>\n",
" <td>g1</td>\n",
" <td>31</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" IPv4 Address abother_column some_column abother_column1 some_column1\n",
"0 69.89.31.226 q 1 qq 11\n",
"1 79.81.31.226 g 3 g1 31"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.merge(df1, df2, on='IPv4 Address', how='inner')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"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.5.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment