redoules.github.io/python/rename_columns_pandas.html
2019-11-25 21:39:34 +01:00

696 lines
21 KiB
HTML

<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<meta name="description" content="Data Science for Political and Social Phenomena">
<meta name="author" content="Guillaume Redoulès">
<link rel="icon" href="../favicon.ico">
<title>Rename columns in pandas - Python</title>
<!-- JQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
window.jQuery || document.write('<script src="../theme/js/jquery.min.js"><\/script>')
</script>
<!-- Bootstrap core CSS -->
<link rel="stylesheet" href="../theme/css/bootstrap.css" />
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<link rel="stylesheet" type="text/css" href="../theme/css/ie10-viewport-bug-workaround.css" />
<!-- Custom styles for this template -->
<link rel="stylesheet" type="text/css" href="../theme/css/style.css" />
<link rel="stylesheet" type="text/css" href="../theme/css/notebooks.css" />
<link href='https://fonts.googleapis.com/css?family=PT+Serif:400,700|Roboto:400,500,700' rel='stylesheet' type='text/css'>
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<meta name="tags" content="Data Wrangling" />
</head>
<body>
<div class="navbar navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="..">Guillaume Redoulès</a>
</div>
<div class="navbar-collapse collapse" id="searchbar">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">About<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="../pages/about.html">About Guillaume</a></li>
<li><a href="https://github.com/redoules">GitHub</a></li>
<li><a href="https://www.linkedin.com/in/guillaume-redoul%C3%A8s-33923860/">LinkedIn</a></li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Data Science<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="..#Blog">Blog</a></li>
<li><a href="..#Python">Python</a></li>
<li><a href="..#Bash">Bash</a></li>
<li><a href="..#SQL">SQL</a></li>
<li><a href="..#Mathematics">Mathematics</a></li>
<li><a href="..#Machine_Learning">Machine Learning</a></li>
<li><a href="..#Projects">Projects</a></li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Projects<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="https://github.com/redoules/redoules.github.io">Notes (Github)</a></li>
</ul>
</li>
<!--<li class="dropdown">
<a href="../feeds/blog.rss.xml">Blog RSS</a>
</li>-->
</ul>
<form class="navbar-form" action="../search.html" onsubmit="return validateForm(this.elements['q'].value);">
<div class="form-group" style="display:inline;">
<div class="input-group" style="display:table;">
<span class="input-group-addon" style="width:1%;"><span class="glyphicon glyphicon-search"></span></span>
<input class="form-control search-query" name="q" id="tipue_search_input" placeholder="e.g. scikit KNN, pandas merge" required autocomplete="off" type="text">
</div>
</div>
</form>
</div>
<!--/.nav-collapse -->
</div>
</div>
<!-- end of header section -->
<div class="container">
<!-- <div class="alert alert-warning" role="alert">
Did you find this page useful? Please do me a quick favor and <a href="#" class="alert-link">endorse me for data science on LinkedIn</a>.
</div> -->
<section id="content" class="body">
<header>
<h1>
Rename columns in pandas
</h1>
<ol class="breadcrumb">
<li>
<time class="published" datetime="2019-08-03T08:11:00+02:00">
03 août 2019
</time>
</li>
<li>Python</li>
<li>Data Wrangling</li>
</ol>
</header>
<div class='article_content'>
<p>In this article, we will be renaming columns in a pandas dataframe.
First, let's import pandas and create an example dataframe</p>
<div class="highlight"><pre><span></span><span class="c1"># Import modules</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="kn">as</span> <span class="nn">pd</span>
</pre></div>
<div class="highlight"><pre><span></span><span class="c1"># Example dataframe</span>
<span class="n">raw_data</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;fruit&#39;</span><span class="p">:</span> <span class="p">[</span><span class="s1">&#39;Banana&#39;</span><span class="p">,</span> <span class="s1">&#39;Orange&#39;</span><span class="p">,</span> <span class="s1">&#39;Apple&#39;</span><span class="p">,</span> <span class="s1">&#39;lemon&#39;</span><span class="p">,</span> <span class="s2">&quot;lime&quot;</span><span class="p">,</span> <span class="s2">&quot;plum&quot;</span><span class="p">],</span>
<span class="s1">&#39;color&#39;</span><span class="p">:</span> <span class="p">[</span><span class="s1">&#39;yellow&#39;</span><span class="p">,</span> <span class="s1">&#39;orange&#39;</span><span class="p">,</span> <span class="s1">&#39;red&#39;</span><span class="p">,</span> <span class="s1">&#39;yellow&#39;</span><span class="p">,</span> <span class="s2">&quot;green&quot;</span><span class="p">,</span> <span class="s2">&quot;purple&quot;</span><span class="p">],</span>
<span class="s1">&#39;kcal&#39;</span><span class="p">:</span> <span class="p">[</span><span class="mi">89</span><span class="p">,</span> <span class="mi">47</span><span class="p">,</span> <span class="mi">52</span><span class="p">,</span> <span class="mi">15</span><span class="p">,</span> <span class="mi">30</span><span class="p">,</span> <span class="mi">28</span><span class="p">]</span>
<span class="p">}</span>
<span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">raw_data</span><span class="p">,</span> <span class="n">columns</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;fruit&#39;</span><span class="p">,</span> <span class="s1">&#39;color&#39;</span><span class="p">,</span> <span class="s1">&#39;kcal&#39;</span><span class="p">])</span>
<span class="n">df</span>
</pre></div>
<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>fruit</th>
<th>color</th>
<th>kcal</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>Banana</td>
<td>yellow</td>
<td>89</td>
</tr>
<tr>
<th>1</th>
<td>Orange</td>
<td>orange</td>
<td>47</td>
</tr>
<tr>
<th>2</th>
<td>Apple</td>
<td>red</td>
<td>52</td>
</tr>
<tr>
<th>3</th>
<td>lemon</td>
<td>yellow</td>
<td>15</td>
</tr>
<tr>
<th>4</th>
<td>lime</td>
<td>green</td>
<td>30</td>
</tr>
<tr>
<th>5</th>
<td>plum</td>
<td>purple</td>
<td>28</td>
</tr>
</tbody>
</table>
</div>
<p>The most flexible method for renaming columns in pandas is the rename method. It takes a dictornnary as an argument where :
* the keys are the old names
* the values are the new names
you also need to specify the axis.</p>
<p>This method can be used to rename either one or multiple columns</p>
<div class="highlight"><pre><span></span><span class="n">df</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">rename</span><span class="p">({</span><span class="s2">&quot;fruit&quot;</span> <span class="p">:</span> <span class="s2">&quot;produce&quot;</span><span class="p">,</span> <span class="s2">&quot;kcal&quot;</span><span class="p">:</span> <span class="s2">&quot;energy&quot;</span><span class="p">},</span> <span class="n">axis</span><span class="o">=</span><span class="s2">&quot;columns&quot;</span><span class="p">)</span>
<span class="n">df</span>
</pre></div>
<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>produce</th>
<th>color</th>
<th>energy</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>Banana</td>
<td>yellow</td>
<td>89</td>
</tr>
<tr>
<th>1</th>
<td>Orange</td>
<td>orange</td>
<td>47</td>
</tr>
<tr>
<th>2</th>
<td>Apple</td>
<td>red</td>
<td>52</td>
</tr>
<tr>
<th>3</th>
<td>lemon</td>
<td>yellow</td>
<td>15</td>
</tr>
<tr>
<th>4</th>
<td>lime</td>
<td>green</td>
<td>30</td>
</tr>
<tr>
<th>5</th>
<td>plum</td>
<td>purple</td>
<td>28</td>
</tr>
</tbody>
</table>
</div>
<p>If you want to rename all the columns at once, a common method is to rewrite the columns attribute of the dataframe</p>
<div class="highlight"><pre><span></span><span class="n">df</span><span class="o">.</span><span class="n">columns</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;nice fruit&quot;</span><span class="p">,</span> <span class="s2">&quot;bright color&quot;</span><span class="p">,</span> <span class="s2">&quot;light kcal&quot;</span><span class="p">]</span>
<span class="n">df</span>
</pre></div>
<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>nice fruit</th>
<th>bright color</th>
<th>light kcal</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>Banana</td>
<td>yellow</td>
<td>89</td>
</tr>
<tr>
<th>1</th>
<td>Orange</td>
<td>orange</td>
<td>47</td>
</tr>
<tr>
<th>2</th>
<td>Apple</td>
<td>red</td>
<td>52</td>
</tr>
<tr>
<th>3</th>
<td>lemon</td>
<td>yellow</td>
<td>15</td>
</tr>
<tr>
<th>4</th>
<td>lime</td>
<td>green</td>
<td>30</td>
</tr>
<tr>
<th>5</th>
<td>plum</td>
<td>purple</td>
<td>28</td>
</tr>
</tbody>
</table>
</div>
<p>If the only thing you are doing is replacing a space with an underscore, an even better method is to use the str.replace method since you don't have to type all the column names</p>
<div class="highlight"><pre><span></span><span class="n">df</span><span class="o">.</span><span class="n">columns</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">columns</span><span class="o">.</span><span class="n">str</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s2">&quot; &quot;</span><span class="p">,</span> <span class="s2">&quot;_&quot;</span><span class="p">)</span>
<span class="n">df</span>
</pre></div>
<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>nice_fruit</th>
<th>bright_color</th>
<th>light_kcal</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>Banana</td>
<td>yellow</td>
<td>89</td>
</tr>
<tr>
<th>1</th>
<td>Orange</td>
<td>orange</td>
<td>47</td>
</tr>
<tr>
<th>2</th>
<td>Apple</td>
<td>red</td>
<td>52</td>
</tr>
<tr>
<th>3</th>
<td>lemon</td>
<td>yellow</td>
<td>15</td>
</tr>
<tr>
<th>4</th>
<td>lime</td>
<td>green</td>
<td>30</td>
</tr>
<tr>
<th>5</th>
<td>plum</td>
<td>purple</td>
<td>28</td>
</tr>
</tbody>
</table>
</div>
<p>Similarly, you can use other str methods such as :
* capitalize : in order to converts first character to capital letter
* lower : in order to have lowercase column names
* upper : in order to have uppercase column names
* etc.</p>
<div class="highlight"><pre><span></span><span class="n">df</span><span class="o">.</span><span class="n">columns</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">columns</span><span class="o">.</span><span class="n">str</span><span class="o">.</span><span class="n">capitalize</span><span class="p">()</span>
<span class="n">df</span>
</pre></div>
<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Nice_fruit</th>
<th>Bright_color</th>
<th>Light_kcal</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>Banana</td>
<td>yellow</td>
<td>89</td>
</tr>
<tr>
<th>1</th>
<td>Orange</td>
<td>orange</td>
<td>47</td>
</tr>
<tr>
<th>2</th>
<td>Apple</td>
<td>red</td>
<td>52</td>
</tr>
<tr>
<th>3</th>
<td>lemon</td>
<td>yellow</td>
<td>15</td>
</tr>
<tr>
<th>4</th>
<td>lime</td>
<td>green</td>
<td>30</td>
</tr>
<tr>
<th>5</th>
<td>plum</td>
<td>purple</td>
<td>28</td>
</tr>
</tbody>
</table>
</div>
<p>Finaly, if you only need to add a prefix or a suffix to the columns, you can use the add_prefix method</p>
<div class="highlight"><pre><span></span><span class="n">df</span><span class="o">.</span><span class="n">add_prefix</span><span class="p">(</span><span class="s2">&quot;pre_&quot;</span><span class="p">)</span>
</pre></div>
<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>pre_Nice_fruit</th>
<th>pre_Bright_color</th>
<th>pre_Light_kcal</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>Banana</td>
<td>yellow</td>
<td>89</td>
</tr>
<tr>
<th>1</th>
<td>Orange</td>
<td>orange</td>
<td>47</td>
</tr>
<tr>
<th>2</th>
<td>Apple</td>
<td>red</td>
<td>52</td>
</tr>
<tr>
<th>3</th>
<td>lemon</td>
<td>yellow</td>
<td>15</td>
</tr>
<tr>
<th>4</th>
<td>lime</td>
<td>green</td>
<td>30</td>
</tr>
<tr>
<th>5</th>
<td>plum</td>
<td>purple</td>
<td>28</td>
</tr>
</tbody>
</table>
</div>
<p>or the add_suffix method</p>
<div class="highlight"><pre><span></span><span class="n">df</span><span class="o">.</span><span class="n">add_suffix</span><span class="p">(</span><span class="s2">&quot;_post&quot;</span><span class="p">)</span>
</pre></div>
<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Nice_fruit_post</th>
<th>Bright_color_post</th>
<th>Light_kcal_post</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>Banana</td>
<td>yellow</td>
<td>89</td>
</tr>
<tr>
<th>1</th>
<td>Orange</td>
<td>orange</td>
<td>47</td>
</tr>
<tr>
<th>2</th>
<td>Apple</td>
<td>red</td>
<td>52</td>
</tr>
<tr>
<th>3</th>
<td>lemon</td>
<td>yellow</td>
<td>15</td>
</tr>
<tr>
<th>4</th>
<td>lime</td>
<td>green</td>
<td>30</td>
</tr>
<tr>
<th>5</th>
<td>plum</td>
<td>purple</td>
<td>28</td>
</tr>
</tbody>
</table>
</div>
</div>
<aside>
<div class="bug-reporting__panel">
<h3>Find an error or bug? Have a suggestion?</h3>
<p>Everything on this site is avaliable on GitHub. Head on over and <a href='https://github.com/redoules/redoules.github.io/issues/new'>submit an issue.</a> You can also message me directly by <a href='mailto:guillaume.redoules@gadz.org'>email</a>.</p>
</div>
</aside>
</section>
</div>
<!-- start of footer section -->
<footer class="footer">
<div class="container">
<p class="text-muted">
<center>This project contains 105 pages and is available on <a href="https://github.com/redoules/redoules.github.io">GitHub</a>.
<br/>
Copyright &copy; Guillaume Redoulès,
<time datetime="2018">2018</time>.
</center>
</p>
</div>
</footer>
<!-- This jQuery line finds any span that contains code highlighting classes and then selects the parent <pre> tag and adds a border. This is done as a workaround to visually distinguish the code inputs and outputs -->
<script>
$( ".hll, .n, .c, .err, .k, .o, .cm, .cp, .c1, .cs, .gd, .ge, .gr, .gh, .gi, .go, .gp, .gs, .gu, .gt, .kc, .kd, .kn, .kp, .kr, .kt, .m, .s, .na, .nb, .nc, .no, .nd, .ni, .ne, .nf, .nl, .nn, .nt, .nv, .ow, .w, .mf, .mh, .mi, .mo, .sb, .sc, .sd, .s2, .se, .sh, .si, .sx, .sr, .s1, .ss, .bp, .vc, .vg, .vi, .il" ).parent( "pre" ).css( "border", "1px solid #DEDEDE" );
</script>
<!-- Load Google Analytics -->
<script>
/*
(function(i, s, o, g, r, a, m) {
i['GoogleAnalyticsObject'] = r;
i[r] = i[r] || function() {
(i[r].q = i[r].q || []).push(arguments)
}, i[r].l = 1 * new Date();
a = s.createElement(o),
m = s.getElementsByTagName(o)[0];
a.async = 1;
a.src = g;
m.parentNode.insertBefore(a, m)
})(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');
ga('create', 'UA-66582-32', 'auto');
ga('send', 'pageview');
*/
</script>
<!-- End of Google Analytics -->
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="../theme/js/bootstrap.min.js"></script>
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<script src="../theme/js/ie10-viewport-bug-workaround.js"></script>
</body>
</html>