Skip to content

Instantly share code, notes, and snippets.

@pybokeh
Last active February 7, 2019 20:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pybokeh/0ed897b5267d893670b2306bf92a9a58 to your computer and use it in GitHub Desktop.
Save pybokeh/0ed897b5267d893670b2306bf92a9a58 to your computer and use it in GitHub Desktop.
pandas/Text_To_Columns.ipynb
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": {},
"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": 4,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "df",
"execution_count": 5,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 5,
"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[['LastName', 'FirstName']] = df['name'].str.split(' ', expand=True)",
"execution_count": 6,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "df",
"execution_count": 7,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 7,
"data": {
"text/plain": " name number LastName FirstName\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>LastName</th>\n <th>FirstName</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": "0ed897b5267d893670b2306bf92a9a58",
"data": {
"description": "pandas/Text_To_Columns.ipynb",
"public": true
}
},
"_draft": {
"nbviewer_url": "https://gist.github.com/0ed897b5267d893670b2306bf92a9a58"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment