Skip to content

Commit

Permalink
feature/StringConcatenator (#52)
Browse files Browse the repository at this point in the history
* StringConcatenator added to strings.py
  • Loading branch information
davidhopkinson26 authored Jan 17, 2023
1 parent 68af2a8 commit 0664167
Show file tree
Hide file tree
Showing 4 changed files with 500 additions and 0 deletions.
301 changes: 301 additions & 0 deletions examples/strings/StringConcatenator.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,301 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# StringConcatenator\n",
"This notebook shows the functionality of the `StringConcatenator` class. This Transformer combines data from specified columns, of mixed datatypes, into a new column containing one string."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import tubular\n",
"from tubular.strings import StringConcatenator"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'0.3.3'"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tubular.__version__"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Create sample data"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"character_df = pd.DataFrame(([':', ')', 5], [':', '(', 3]), columns=['c1', 'c2', 'c3'])"
]
},
{
"cell_type": "code",
"execution_count": 5,
"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>c1</th>\n",
" <th>c2</th>\n",
" <th>c3</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>:</td>\n",
" <td>)</td>\n",
" <td>5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>:</td>\n",
" <td>(</td>\n",
" <td>3</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" c1 c2 c3\n",
"0 : ) 5\n",
"1 : ( 3"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"character_df.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Simple usage"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Initialising StringConcatenatorTransformer"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The user must specify the following;\n",
"- `columns` giving the columns to join\n",
"- `new column` giving the name of the new column\n",
"- `separator` giving the separator for joining (optional)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"to_merge = ['c1', 'c2', 'c3']\n",
"\n",
"join_columns = StringConcatenator(to_merge, \"merged\", \"-\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### StringConcatenatorTransformer fit\n",
"There is no fit method for the StringConcatenatorTransformer as the methods that it can run do not 'learn' anything from the data."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### StringConcatenatorTransformer transform"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"joined_df = join_columns.transform(character_df)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"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>c1</th>\n",
" <th>c2</th>\n",
" <th>c3</th>\n",
" <th>merged</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>:</td>\n",
" <td>)</td>\n",
" <td>5</td>\n",
" <td>:-)-5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>:</td>\n",
" <td>(</td>\n",
" <td>3</td>\n",
" <td>:-(-3</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" c1 c2 c3 merged\n",
"0 : ) 5 :-)-5\n",
"1 : ( 3 :-(-3"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"joined_df"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.8.0 ('tubular-dev')",
"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.8.0"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {
"height": "calc(100% - 180px)",
"left": "10px",
"top": "150px",
"width": "384px"
},
"toc_section_display": true,
"toc_window_display": true
},
"vscode": {
"interpreter": {
"hash": "cc666868ff21538e6058ba6d4768423bd0d0d7d7fded3ffb1bc309a0bf9339c2"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading

0 comments on commit 0664167

Please sign in to comment.