Skip to content

Instantly share code, notes, and snippets.

@NeroHin
Created October 30, 2022 11:44
Show Gist options
  • Save NeroHin/621d595a477599021b8f6a4d0b157704 to your computer and use it in GitHub Desktop.
Save NeroHin/621d595a477599021b8f6a4d0b157704 to your computer and use it in GitHub Desktop.
regex_pandas_python
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import re"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 結合 Pandas 和 Series"
]
},
{
"cell_type": "code",
"execution_count": 27,
"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>A</th>\n",
" <th>B</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1.2&gt;1.3&gt;1.4(by 護理長)</td>\n",
" <td>1.2&gt;1.3&gt;1.6</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1.2&gt;1.3&gt;1.5</td>\n",
" <td>1.2&gt;1.3&gt;1.7</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" A B\n",
"0 1.2>1.3>1.4(by 護理長) 1.2>1.3>1.6\n",
"1 1.2>1.3>1.5 1.2>1.3>1.7"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = pd.DataFrame({\n",
" 'A' : ['1.2>1.3>1.4(by 護理長)', '1.2>1.3>1.5', '1.2>1.3>1.9'],\n",
" 'B' : ['1.2>1.3>1.6', '1.2>1.3>1.7', '1.2>1.3>1.8(by 護理長)'],\n",
"})\n",
"\n",
"df.head(2)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Pandas Series"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 1.4\n",
"1 1.5\n",
"2 1.9\n",
"Name: A, dtype: object"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"strip_split_symbol = lambda x: re.sub(r'[^0-9.>]', '', x).split('>')[-1]\n",
"\n",
"df['A'] = df['A'].apply(strip_split_symbol)\n",
"\n",
"df.A"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Pandas DataFrame"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"# with dataframe\n",
"for col in df.columns:\n",
" df[col] = df[col].apply(strip_split_symbol)"
]
},
{
"cell_type": "code",
"execution_count": 30,
"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>A</th>\n",
" <th>B</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1.4</td>\n",
" <td>1.6</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1.5</td>\n",
" <td>1.7</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1.9</td>\n",
" <td>1.8</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" A B\n",
"0 1.4 1.6\n",
"1 1.5 1.7\n",
"2 1.9 1.8"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.8.12 ('api_server')",
"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.12"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "8ba975e9754e85fb30fc4fec1d82e9cb6c3031376e8f82bbcac6e0a1936ff599"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment