How to Replace Items in a Python List

You can use a list comprehension to replace items in a Python list: Copy my_list = [‘item 1’, ‘item 2’, ‘item 3’,…] my_list = [‘new item’ if i==’old item’ else i for i in my_list] To better understand how to replace items in a Python list, you’ll see the following 3 scenarios about: Replacing an … Read more

Replace Characters in Strings in Pandas DataFrame

Here are two ways to replace characters in strings in Pandas DataFrame: (1) Replace character/s under a single DataFrame column: Copy df[‘column name’] = df[‘column name’].str.replace(‘old character’,’new character’) (2) Replace character/s under the entire DataFrame: Copy df = df.replace(‘old character’,’new character’, regex=True) In this short guide, you’ll see how to replace: Specific character under a … Read more

How to Control a Mouse using Python

The PyAutoGUI library can be used to control a mouse in Python. To install the PyAutoGUI library: Copy pip install pyautogui Next, you’ll see 4 scenarios to control a mouse in Python by: 4 Scenarios of Controlling a Mouse using Python Scenario 1: Moving a mouse cursor to a specific location To move a mouse … Read more

How to Convert NumPy Array to Pandas DataFrame

In this short guide, you’ll see how to convert a NumPy array to Pandas DataFrame. Here are the steps: Steps to Convert a NumPy Array to Pandas DataFrame Step 1: Create a NumPy Array For example, let’s create the following NumPy array that contains only numeric data (i.e., integers): Copy import numpy as np my_array … Read more

How to Convert Pandas DataFrame to NumPy Array

Here are two approaches to convert Pandas DataFrame to a NumPy array: (1) First approach: Copy df.to_numpy() (2) Second approach: Copy df.values Steps to Convert Pandas DataFrame to a NumPy Array Step 1: Create a DataFrame To start with a simple example, let’s create a DataFrame with 3 columns. The 3 columns will contain only … Read more

Delete Records in SQL Server using Python

In this guide, you’ll see how to delete records in SQL Server directly from Python. Here are the steps that you may follow. Steps to Delete Records in SQL Server using Python Step 1: Install the Pyodbc Library If you haven’t already done so, install the pyodbc library using the following command: Copy pip install … Read more

Select all Rows with NaN Values in Pandas DataFrame

Here are 4 ways to select all rows with NaN values in Pandas DataFrame: (1) Using isna() to select all rows with NaN under a single DataFrame column: Copy df[df[‘column name’].isna()] (2) Using isnull() to select all rows with NaN under a single DataFrame column: Copy df[df[‘column name’].isnull()] (3) Using isna() to select all rows … Read more

Filter Pandas DataFrame Based on Index

To filter Pandas DataFrame based on the index: Copy df_filter = df.filter(items=[index to keep], axis=0) Example Suppose that you created the DataFrame below: Copy import pandas as pddata = { “Product”: [“Computer”, “Printer”, “Monitor”, “Desk”, “Phone”, “Tablet”, “Scanner”], “Price”: [900, 200, 300, 450, 150, 250, 150],}df = pd.DataFrame(data)print(df) As you can see in yellow, the … Read more

Check if Pandas DataFrame is Empty

You can use df.empty to check if Pandas DataFrame is empty: Copy df.empty Where: Steps to Check if Pandas DataFrame is Empty Step 1: Create a DataFrame To start with a simple example, let’s create a DataFrame with 2 columns: Copy import pandas as pd data = {‘Color’: [‘Blue’, ‘Blue’, ‘Green’, ‘Green’, ‘Red’, ‘Red’], ‘Height’: … Read more

How to Find Where Python is Installed on Windows

In this short guide, you’ll see two methods to find where Python is installed on Windows: Find Where Python is Installed on Windows using the Sys Library You can use the sys library in order to find where Python is installed: Copy import sys print(sys.exec_prefix) Here is an example of a path structure that you … Read more