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

How to Remove a Trailing Newline?

Use str.rstrip() to remove a trailing newline. Given a string s, create a new one without trailing newline character by calling s.rstrip(‘\n’). If you need the string stored in the original variable, simply assign the result to the original variable. Here’s an example: This actually removes all trailing newline characters: If you want to remove … Read more

Pandas drop_level(), pivot(), pivot_table(), reorder_levels(), sort_values(), sort_index()

The Pandas DataFrame/Series has several methods to handle Missing Data. When applied to a DataFrame/Series, these methods evaluate and modify the missing elements. 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 multi-dimensional arrays and matrices in addition … Read more

Python __aiter__() and __anext__() Magic Methods

object.__aiter__(self) object.__anext__(self) πŸ’‘ Summary: Python’s __aiter__() and __anext__() methods are used to implement an asynchronous for loop (keywords: async for). In contrast to a normal (synchronous) for loop, an asynchronous for loop iterates over an asynchronous source. __aiter__() returns an asynchronous iterator object (in many cases it’s simply a reference to itself: return self) __anext__() … Read more

Handling Missing Data in Pandas: isna(), isnull(), notna(), notnull(), pad(), replace()

The Pandas DataFrame/Series has several methods to handle Missing Data. When applied to a DataFrame/Series, these methods evaluate and modify the missing elements. 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 multi-dimensional arrays and matrices in addition … Read more