Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added trick to Split (explode) pandas string entry to separate rows #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
351 changes: 351 additions & 0 deletions Code/.ipynb_checkpoints/str.split()-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,351 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# import necessary libraries\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 6,
"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>name</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Elise Mccann</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Aiden Berger</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Elle Kelley</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" name\n",
"0 Elise Mccann\n",
"1 Aiden Berger\n",
"2 Elle Kelley"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# create a dataframe\n",
"df = pd.DataFrame({'name':['Elise Mccann', 'Aiden Berger', 'Elle Kelley']})\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": 3,
"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>name</th>\n",
" <th>first_name</th>\n",
" <th>last_name</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Elise Mccann</td>\n",
" <td>Elise</td>\n",
" <td>Mccann</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Aiden Berger</td>\n",
" <td>Aiden</td>\n",
" <td>Berger</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Elle Kelley</td>\n",
" <td>Elle</td>\n",
" <td>Kelley</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" name first_name last_name\n",
"0 Elise Mccann Elise Mccann\n",
"1 Aiden Berger Aiden Berger\n",
"2 Elle Kelley Elle Kelley"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# extract first name and last name\n",
"df['first_name'] = df['name'].str.split(' ', expand = True)[0]\n",
"df['last_name'] = df['name'].str.split(' ', expand = True)[1]\n",
"\n",
"df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Split (explode) pandas dataframe string entry to separate rows"
]
},
{
"cell_type": "code",
"execution_count": 10,
"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>name</th>\n",
" <th>Participation in</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Elise Mccann</td>\n",
" <td>Chess,Football</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Aiden Berger</td>\n",
" <td>Cricket</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Elle Kelley</td>\n",
" <td>Foosball,Carrom</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" name Participation in\n",
"0 Elise Mccann Chess,Football\n",
"1 Aiden Berger Cricket\n",
"2 Elle Kelley Foosball,Carrom"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dx = pd.DataFrame({'name':['Elise Mccann', 'Aiden Berger', 'Elle Kelley'],\n",
" 'Participation in': ['Chess,Football','Cricket','Foosball,Carrom']})\n",
"dx"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"def comma_separator(df, col):\n",
" constraints=df[col].apply(lambda x: str(x).split(',')).tolist()\n",
" df_new=pd.DataFrame(constraints, index=df['name'])\n",
" df_new=df_new.apply(pd.Series).stack()\n",
" df_new=pd.DataFrame(df_new)\n",
" df_new.reset_index(inplace=True)\n",
" df_new=df_new[['name',0]]\n",
" df_new.columns=['name','Participated in'] \n",
" df_new=df_new[~(df_new['Participated in']=='None')]\n",
" return df_new"
]
},
{
"cell_type": "code",
"execution_count": 12,
"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>name</th>\n",
" <th>Participated in</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Elise Mccann</td>\n",
" <td>Chess</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Elise Mccann</td>\n",
" <td>Football</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Aiden Berger</td>\n",
" <td>Cricket</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Elle Kelley</td>\n",
" <td>Foosball</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Elle Kelley</td>\n",
" <td>Carrom</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" name Participated in\n",
"0 Elise Mccann Chess\n",
"1 Elise Mccann Football\n",
"2 Aiden Berger Cricket\n",
"3 Elle Kelley Foosball\n",
"4 Elle Kelley Carrom"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"comma_separator(dx,'Participation in')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}