Skip to content

Instantly share code, notes, and snippets.

@amdevine
Created March 31, 2021 02:17
Show Gist options
  • Select an option

  • Save amdevine/887758f117328854ce980fbd1b1a3ca7 to your computer and use it in GitHub Desktop.

Select an option

Save amdevine/887758f117328854ce980fbd1b1a3ca7 to your computer and use it in GitHub Desktop.
Modify Pandas DataFrame column and index labels with functions
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Import Pandas"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Create example DataFrame"
]
},
{
"cell_type": "code",
"execution_count": 2,
"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>DAY</th>\n",
" <th>WEATHER</th>\n",
" <th>HIGH</th>\n",
" <th>LOW</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Monday</td>\n",
" <td>Sunny</td>\n",
" <td>70</td>\n",
" <td>52</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Tuesday</td>\n",
" <td>Sunny</td>\n",
" <td>64</td>\n",
" <td>56</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Wednesday</td>\n",
" <td>Sunny</td>\n",
" <td>66</td>\n",
" <td>56</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Thursday</td>\n",
" <td>Rain</td>\n",
" <td>72</td>\n",
" <td>61</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Friday</td>\n",
" <td>Rain</td>\n",
" <td>75</td>\n",
" <td>59</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>Saturday</td>\n",
" <td>Partly Cloudy</td>\n",
" <td>71</td>\n",
" <td>51</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>Sunday</td>\n",
" <td>Partly Cloudy</td>\n",
" <td>60</td>\n",
" <td>42</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" DAY WEATHER HIGH LOW\n",
"0 Monday Sunny 70 52\n",
"1 Tuesday Sunny 64 56\n",
"2 Wednesday Sunny 66 56\n",
"3 Thursday Rain 72 61\n",
"4 Friday Rain 75 59\n",
"5 Saturday Partly Cloudy 71 51\n",
"6 Sunday Partly Cloudy 60 42"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"weather = pd.DataFrame({'DAY': ['Monday', 'Tuesday', 'Wednesday', 'Thursday', \n",
" 'Friday', 'Saturday', 'Sunday'],\n",
" 'WEATHER': (['Sunny']*3 + ['Rain']*2 + \n",
" ['Partly Cloudy']*2),\n",
" 'HIGH': [70, 64, 66, 72, 75, 71, 60],\n",
" 'LOW': [52, 56, 56, 61, 59, 51, 42]})\n",
"weather"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Change column names and index using functions\n",
"\n",
"Columns: change column name to titlecase using `str.title()`\n",
"\n",
"Index: If we want to start the index at 10 (because e.g. Monday is the 10th day of the month), we can use a lambda function to add 10 to every value in the current index."
]
},
{
"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>Day</th>\n",
" <th>Weather</th>\n",
" <th>High</th>\n",
" <th>Low</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>Monday</td>\n",
" <td>Sunny</td>\n",
" <td>70</td>\n",
" <td>52</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>Tuesday</td>\n",
" <td>Sunny</td>\n",
" <td>64</td>\n",
" <td>56</td>\n",
" </tr>\n",
" <tr>\n",
" <th>12</th>\n",
" <td>Wednesday</td>\n",
" <td>Sunny</td>\n",
" <td>66</td>\n",
" <td>56</td>\n",
" </tr>\n",
" <tr>\n",
" <th>13</th>\n",
" <td>Thursday</td>\n",
" <td>Rain</td>\n",
" <td>72</td>\n",
" <td>61</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14</th>\n",
" <td>Friday</td>\n",
" <td>Rain</td>\n",
" <td>75</td>\n",
" <td>59</td>\n",
" </tr>\n",
" <tr>\n",
" <th>15</th>\n",
" <td>Saturday</td>\n",
" <td>Partly Cloudy</td>\n",
" <td>71</td>\n",
" <td>51</td>\n",
" </tr>\n",
" <tr>\n",
" <th>16</th>\n",
" <td>Sunday</td>\n",
" <td>Partly Cloudy</td>\n",
" <td>60</td>\n",
" <td>42</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Day Weather High Low\n",
"10 Monday Sunny 70 52\n",
"11 Tuesday Sunny 64 56\n",
"12 Wednesday Sunny 66 56\n",
"13 Thursday Rain 72 61\n",
"14 Friday Rain 75 59\n",
"15 Saturday Partly Cloudy 71 51\n",
"16 Sunday Partly Cloudy 60 42"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"weather2 = weather.rename(columns=str.title, \n",
" index=lambda x: x+10)\n",
"weather2"
]
}
],
"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.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment