One Line of Code Every Day: A Powerful Habit

Today I want to propose a new habit for you which will ensure that you are on the path of continuous improvement in Python — on the path to mastery. Did you read the book “The Power of Habit” by Charles Duhigg? This Keystone Habit changed her life… The Pulitzer-Price-winning author talks about forming so-called … Read more

How To Apply A Function To Each Element Of A Tuple?

This article shows you how to apply a given function to each element of a tuple. The best way to apply a function to each element of a tuple is the Python built-in map(function, iterable) function that takes a function and an iterable as arguments and applies the function to each iterable element. An alternate … Read more

Pandas melt(), explode(), squeeze(), to_xarray(), transpose()

The Pandas DataFrame/Series has several methods to re-shape, sort, and transpose the data. Preparation Before any data manipulation can occur, two (2) new libraries will require installation. The Pandas library enables access to/from a DataFrame. The Xarray library works with labeled multi-dimensional arrays and advanced analytics. To install these libraries, navigate to an IDE terminal. … Read more

Replace Last Substring Occurrence in Python String

Problem Formulation Given a string, a substring, and a replacement string in Python. String s Substring sub Replacement string repl How to find and replace the last occurrence of sub with the replacement repl in the Python string s? Let’s have a look at a couple of examples to thoroughly understand the problem: Example 1: … Read more

Find Index of Last Substring Occurrence in Python String

Problem Formulation Given a string and a substring in Python. How to find the index of the last occurrence of the substring in Python? Let’s have a look at a couple of examples to thoroughly understand the problem: Example 1: string = ‘fifi’ substring = ‘fi’ result: 2 Example 2: string = ‘hello’ substring = … Read more

Pandas get_dummies() – A Simple Guide with Video

In this tutorial, we will learn all about the Pandas function get_dummies(). This method converts categorical data into dummy or indicator variables. Here are the parameters from the official documentation: Parameter Type Description data array-like, Series, or DataFrame Data of which to get the dummy indicators. prefix str, list of str, or dict of str,default … Read more

How to List All Files ONLY in the Current Directory?

Problem Formulation How to list all files in the current directory given these constraints: Non-Recursive: You do not want to list files in subdirectories. ONLY Files: You do not want to list folder names. Current Directory: You run the Python script from the current directory. Here’s an example structure: current_folder — code.py — file.txt — … Read more

Pandas nlargest(), nsmallest(), swap_level(), stack(), unstack(), swap_axes()

The Pandas DataFrame/Series has several methods to re-shape, sort, and transpose the data. When applied to a DataFrame/Series, these methods evaluate and modify the data to accommodate the selections. Preparation Before any data manipulation can occur, two (2) new libraries will require installation. The Pandas library enables access to/from a DataFrame. The NumPy library supports … Read more

How to Make a Beep Sound in Python? [Linux/macOS/Win]

Windows: How to Make Beep in Python To make a beep sound in Python on your Windows machine: Import the library using: import winsound Call windsound.Beep(frequency, duration) for your desired frequency (in Hertz) and duration (in milliseconds). For example, winsound.Beep(2000, 1500) would make a sound with 2000 Hz for 1.5 seconds. Here’s the relevant code … Read more