Last active
February 7, 2019 21:00
-
-
Save pybokeh/a48c94aa8be51d8bcc1d93e08d8e5ea7 to your computer and use it in GitHub Desktop.
pandas/Text_To_Columns.ipynb
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": [ | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "import pandas as pd", | |
"execution_count": 1, | |
"outputs": [] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "### Create a pandas dataframe \"by hand\":" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "df = pd.DataFrame({'name': ['John Doe', 'Jane Doe', 'Someone Else'], 'number': [123, 456, 789]})", | |
"execution_count": 10, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "df", | |
"execution_count": 11, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 11, | |
"data": { | |
"text/plain": " name number\n0 John Doe 123\n1 Jane Doe 456\n2 Someone Else 789", | |
"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>name</th>\n <th>number</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>John Doe</td>\n <td>123</td>\n </tr>\n <tr>\n <th>1</th>\n <td>Jane Doe</td>\n <td>456</td>\n </tr>\n <tr>\n <th>2</th>\n <td>Someone Else</td>\n <td>789</td>\n </tr>\n </tbody>\n</table>\n</div>" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "### Use split() function with ```expand=True``` option:" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# \"unpack\"/create 2 columns into LastName and FirstName columns\ndf[['FirstName', 'LastName']] = df['name'].str.split(' ', expand=True)", | |
"execution_count": 12, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "df", | |
"execution_count": 13, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 13, | |
"data": { | |
"text/plain": " name number FirstName LastName\n0 John Doe 123 John Doe\n1 Jane Doe 456 Jane Doe\n2 Someone Else 789 Someone Else", | |
"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>name</th>\n <th>number</th>\n <th>FirstName</th>\n <th>LastName</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>John Doe</td>\n <td>123</td>\n <td>John</td>\n <td>Doe</td>\n </tr>\n <tr>\n <th>1</th>\n <td>Jane Doe</td>\n <td>456</td>\n <td>Jane</td>\n <td>Doe</td>\n </tr>\n <tr>\n <th>2</th>\n <td>Someone Else</td>\n <td>789</td>\n <td>Someone</td>\n <td>Else</td>\n </tr>\n </tbody>\n</table>\n</div>" | |
}, | |
"metadata": {} | |
} | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3", | |
"language": "python" | |
}, | |
"hide_input": false, | |
"language_info": { | |
"name": "python", | |
"version": "3.7.0", | |
"mimetype": "text/x-python", | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"pygments_lexer": "ipython3", | |
"nbconvert_exporter": "python", | |
"file_extension": ".py" | |
}, | |
"gist": { | |
"id": "a48c94aa8be51d8bcc1d93e08d8e5ea7", | |
"data": { | |
"description": "pandas/Text_To_Columns.ipynb", | |
"public": true | |
} | |
}, | |
"_draft": { | |
"nbviewer_url": "https://gist.github.com/a48c94aa8be51d8bcc1d93e08d8e5ea7" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment