<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: John Higuita</title>
    <description>The latest articles on Forem by John Higuita (@jfhiguita).</description>
    <link>https://forem.com/jfhiguita</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F537784%2F037daa06-7b55-44bf-9552-40f063fba9cc.jpeg</url>
      <title>Forem: John Higuita</title>
      <link>https://forem.com/jfhiguita</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/jfhiguita"/>
    <language>en</language>
    <item>
      <title>Manage environment variables with decouple in Python</title>
      <dc:creator>John Higuita</dc:creator>
      <pubDate>Thu, 18 Feb 2021 05:40:58 +0000</pubDate>
      <link>https://forem.com/jfhiguita/manage-environment-variables-with-decouple-in-python-h20</link>
      <guid>https://forem.com/jfhiguita/manage-environment-variables-with-decouple-in-python-h20</guid>
      <description>&lt;p&gt;In this post we're going to talk about how to set up environment variables for any Python project that we develop.&lt;/p&gt;

&lt;p&gt;Python-decouple is a library that helps you separate the settings parameters from your source code. Its magic appears when the parameters related to an instance of the project, goes to an environment file. The parameters related to the project, goes straight to the source code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Install
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;$ pip install python-decouple&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Usage
&lt;/h2&gt;

&lt;p&gt;The first one create a &lt;code&gt;.env&lt;/code&gt; file in the root of your project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;TOKEN_KEY = 1232331sadsads8768:dsdsds32434-LJSAS08f
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you are working with Git, update your &lt;code&gt;.gitignore&lt;/code&gt; adding the &lt;code&gt;.env&lt;/code&gt; file.&lt;br&gt;
Then let's create &lt;code&gt;cfg.py&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from decouple import config
TOKEN = config('TOKEN_KEY')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And finally, you already can to use the project' parameters in your &lt;code&gt;main.py&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from cfg import TOKEN
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>python</category>
      <category>decouple</category>
      <category>environment</category>
    </item>
    <item>
      <title>Change column type in pandas to numeric (particular case)</title>
      <dc:creator>John Higuita</dc:creator>
      <pubDate>Thu, 10 Dec 2020 15:11:11 +0000</pubDate>
      <link>https://forem.com/jfhiguita/change-column-type-in-pandas-to-numeric-particular-case-48ml</link>
      <guid>https://forem.com/jfhiguita/change-column-type-in-pandas-to-numeric-particular-case-48ml</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BFxMtA4u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/562vvfmkr51besdajt2o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BFxMtA4u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/562vvfmkr51besdajt2o.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;to convert the columns to numeric types, you have four main options:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;to_numeric()&lt;/li&gt;
&lt;li&gt;astype()&lt;/li&gt;
&lt;li&gt;infer_objects()&lt;/li&gt;
&lt;li&gt;convert_dtypes()&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;however, none of the four works in this case because each value has a thousands separator with a comma and a decimal separator with a dot, so this causes error.&lt;/p&gt;

&lt;p&gt;You must delete the comma and then you can convert it to numeric.&lt;/p&gt;

&lt;p&gt;df[column].str.replace(",", "").astype(float)&lt;/p&gt;

</description>
      <category>python</category>
      <category>pandas</category>
      <category>dataframe</category>
      <category>types</category>
    </item>
  </channel>
</rss>
