Multiple Ways to Get the First Row for Each Group in Pandas

Use case Sometimes you just want to capture the first (or last) event of something. Let’s say, you have a list of clients and want to capture their first purchase. This is useful if you want a list of new paying customers. Dataset We’re thinking about customers here, so let’s get the Online Retail Dataset from the UCI Machine Learning Repository. We can download this dataset directly using Pandas. >>> import pandas as pd >>> customers = pd....

<span title='2020-12-03 19:12:26 -0600 -0600'>December 3, 2020</span>&nbsp;·&nbsp;7 min&nbsp;·&nbsp;Franccesco Orozco
Image by @ingoschulz

Python Logging Basics: Why is it important and how to use it?

If you find yourself having troubles debugging your code, or wondering what went wrong, then you should start logging events in your python code. Using the logging library you can basically record what actions is your code doing, i.e making a web request, reading a file, monitoring something, etc. It can help you to narrow down your faulty code for debugging. Moreover, logging is not only helpful for debugging, but it is also helpful for collaboration, and many platforms use the logging module in your code so you navigate between events easily....

<span title='2020-06-27 09:45:41 +0000 UTC'>June 27, 2020</span>&nbsp;·&nbsp;5 min&nbsp;·&nbsp;Franccesco Orozco

Save Python Objects with Pickle

Sometimes you just want to save a dictionary, a list, a string, or anything into a file so you can use it later. This can be easily achieved with the module Pickle. Warning: The pickle module is not secure against erroneous or maliciously constructed data. Never unpickle data received from an untrusted or unauthenticated source. — Pickle Documentation What is Pickle Pickle is a module in Python that can be implemented to serialize or de-serialize a Python object, meaning that it can be used to save an object into a file; Just have in mind that this is not the same as saving a configuration file, there are other data structures that we can use to achieve that task such as JSON, CSV, YAML/TOML, etc....

<span title='2019-06-26 16:21:45 +0000 UTC'>June 26, 2019</span>&nbsp;·&nbsp;3 min&nbsp;·&nbsp;Franccesco Orozco

Develop and Publish Your Python Packages with Poetry

I’ve been trying to publish my packages to PyPi so people can access my software more easily. But I have to be honest, Python’s publishing system is not the best out there and it has to improve quite a lot. Wandering around I stumbled upon Poetry, which is a python packager and dependency manager created by Sébastien Eustace for people that doesn’t want to lose their head managing a Python project....

<span title='2019-06-16 11:20:44 +0000 UTC'>June 16, 2019</span>&nbsp;·&nbsp;6 min&nbsp;·&nbsp;Franccesco Orozco

How to Easily Use a Progress Bar in Python

We’ve all been there, your code is performing a job and it’s going to take a while. I’m an impatient guy, so it would be nice to have an ETA or a progress bar to show us. Fortunately, there are libraries out there than can help us to achieve this! There’s two ways in which we can integrate a progress bar into our loops, via a Context Manager or just wrapping up an iterable object into a method....

<span title='2019-06-15 20:40:39 +0000 UTC'>June 15, 2019</span>&nbsp;·&nbsp;6 min&nbsp;·&nbsp;Franccesco Orozco

Change Flask Root Folder for Templates and Static Files

Today I was facing a problem, I didn’t know how to change Flask root directory. Flask by default look for templates and static files under the root directory (/), how can we change that? Changing the root path Here’s my directory structure: . ├── api_files │ ├── static │ │ └── style.css │ └── templates │ └── index.html ├── api.py ├── Pipfile └── Pipfile.lock 3 directories, 5 files I want Flask to be able to process the folders static and templates inside the api_files folder, my main Flask app is api....

<span title='2018-05-12 13:55:57 +0000 UTC'>May 12, 2018</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;Franccesco Orozco

Deploy a Flask Project to Heroku

I was suddenly in the need of deploying a very basic Flask API to the cloud, so it can be available to the public. The thing is, Flask is not made for a production and scalable environment, but if you only need to deploy a very basic web server to Heroku then this guide is for you. Initialize a repository First of all we will need to set up a virtual enviroment with Pipenv, a Flask app, and initialize a repository....

<span title='2018-05-10 21:26:58 +0000 UTC'>May 10, 2018</span>&nbsp;·&nbsp;3 min&nbsp;·&nbsp;Franccesco Orozco

Importing Variables from a Website

Before we start: A short disclaimer Don’t do this. Don’t go there importing random code you found on the internet into your code because that can be dangerous and code would be injected into your machine or a client’s machine and you don’t want that. Remember to always have your code and modules in a version control system and that you have complete knowledge of what you’re loading. Today I was talking with some of the guys in a Discord server about python, called Python Discord (which I definitely recommend you to check out!...

<span title='2018-04-17 11:03:45 +0000 UTC'>April 17, 2018</span>&nbsp;·&nbsp;5 min&nbsp;·&nbsp;Franccesco Orozco

Create Multiple Directories With Makedirs in Python

You don’t need a bunch of if’s and else if’s to create an array of directories in python, just the good ol’ makedirs. Create an array of directories To create an array of directories you must import the package os and import the method makedirs >>> from os import makedirs >>> makedirs('1/2/3/4/5') You’ll see now that your directories are created correctly, let’s run the command tree outside of python to see if they’re were actually created....

<span title='2018-04-17 08:43:51 +0000 UTC'>April 17, 2018</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;Franccesco Orozco

How to Handle a Deque in Python

Deques are a great way to handle memory efficient appends to a list-like object, it is a special module that allows you to handle list items in a more appropriate way. Create a deque To create a list simply import the deque module from collections library and call deque(_items_) on a variable. >>> from collections import deque >>> dq = deque('123') >>> dq deque(['1', '2', '3']) >>> type(dq) <class 'collections.deque'> Or if you wish to create an empty deque....

<span title='2018-04-14 15:14:58 +0000 UTC'>April 14, 2018</span>&nbsp;·&nbsp;3 min&nbsp;·&nbsp;Franccesco Orozco