Skip to content

Instantly share code, notes, and snippets.

@ericmjl
Created May 18, 2020 10:48
Show Gist options
  • Save ericmjl/83ffa4fe21d0c9220da6159b39d629f3 to your computer and use it in GitHub Desktop.
Save ericmjl/83ffa4fe21d0c9220da6159b39d629f3 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" first_name _last_name _year_of_birth _year_of_death\n",
"0 Isaac Newton 1642 1726\n",
"1 Albert Einstein 1879 1955\n",
"2 James Maxwell 1831 1879\n",
"3 Henri Poincaré 1854 1912\n"
]
}
],
"source": [
"from io import StringIO\n",
"import pandas as pd\n",
"import janitor\n",
"\n",
"donnees = \"\"\"First name, Last name, Year of birth, Year of death\n",
"Isaac, Newton, 1642, 1726\n",
"Albert, Einstein, 1879, 1955\n",
"James, Maxwell, 1831, 1879\n",
"Henri, Poincaré, 1854, 1912\"\"\"\n",
"\n",
"df = pd.read_csv(StringIO(donnees)).clean_names()\n",
"\n",
"column_name = \"_year_of_birth\"\n",
"function = lambda x: x - 18\n",
"\n",
"print(df)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" first_name _last_name _year_of_birth _year_of_death\n",
"0 Isaac Newton 1642 1726\n",
"1 Albert Einstein 1879 1955\n",
"2 James Maxwell 1831 1879\n",
"3 Henri Poincaré 1854 1912\n"
]
}
],
"source": [
"df.transform_column(\"_year_of_birth\", lambda x: x - 18)\n",
"print(df)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" first_name _last_name _year_of_birth _year_of_death\n",
"0 Isaac Newton 1642 1726\n",
"1 Albert Einstein 1879 1955\n",
"2 James Maxwell 1831 1879\n",
"3 Henri Poincaré 1854 1912\n"
]
}
],
"source": [
"janitor.functions.transform_column(df, \"_year_of_birth\", lambda x: x + 18)\n",
"print(df)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" first_name _last_name _year_of_birth _year_of_death\n",
"0 Isaac Newton 1642 1726\n",
"1 Albert Einstein 1879 1955\n",
"2 James Maxwell 1831 1879\n",
"3 Henri Poincaré 1854 1912\n"
]
}
],
"source": [
"dest_column_name = None\n",
"elementwise = True\n",
"if dest_column_name is None:\n",
" dest_column_name = column_name\n",
"\n",
"if elementwise:\n",
" result = df[column_name].apply(function)\n",
"else:\n",
" result = function(df[column_name])\n",
"\n",
"df.assign(**{dest_column_name: result})\n",
"print(df)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "pyjanitor-dev",
"language": "python",
"name": "pyjanitor-dev"
},
"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.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment