Assign New Column to Pandas DataFrame

To assign a new column to Pandas DataFrame: Copy df = df.assign(new_column_name=[“item_1”, “item_2”, “item_3”, …]) Case 1: Assign a Single Column to Pandas DataFrame To start with a simple example, create a DataFrame with a single column about products: Copy import pandas as pddata = {“product_name”: [“tablet”, “printer”, “laptop”, “monitor”]}df = pd.DataFrame(data)print(df) This is how … Read more

Update Records in SQL Server using Python

In this guide, you’ll see the steps to update records in SQL Server using Python. Here are the steps that you may follow. Steps to Update Records in SQL Server using Python Step 1: Install the Pyodbc Package To start, install the pyodbc package using this command: Copy pip install pyodbc Step 2: Connect Python … Read more

Sum each Column and Row in Pandas DataFrame

To sum each numeric column and row in Pandas DataFrame (make sure to apply it to numeric values): (1) Sum each column: Copy df.sum(axis=0) (2) Sum each row: Copy df.sum(axis=1) Steps to Sum each Column and Row in Pandas DataFrame Step 1: Prepare the Data For example, here is a simple dataset that contains information … Read more

How to Create a Calculator using Python

In this short guide, you’ll see how to create a simple calculator using Python. The Complete Code Here is the complete code to create a simple calculator using Python. Copy import tkinter as tkroot = tk.Tk()canvas1 = tk.Canvas(root, width=400, height=400)canvas1.pack()entry1 = tk.Entry(root)canvas1.create_window(230, 100, window=entry1)entry2 = tk.Entry(root)canvas1.create_window(230, 140, window=entry2)label0 = tk.Label(root, text=”Calculator”)label0.config(font=(“arial”, 14))canvas1.create_window(200, 40, window=label0)label1 = … Read more

Create Executable of Python Script using PyInstaller

In this short guide, you’ll see the how to create an executable of a Python script using PyInstaller. The following video tutorial is also available: Steps to Create an Executable using PyInstaller Step 1: Add Python to Windows Path To start, you may want to add Python to Windows path. An easy way to add … Read more

Count Duplicates in Pandas DataFrame

You can count duplicates in Pandas DataFrame using this approach: Copy df.pivot_table(columns=[‘DataFrame Column’], aggfunc=’size’) In this short guide, you’ll see 3 cases of counting duplicates in Pandas DataFrame: 3 Cases of Counting Duplicates in Pandas DataFrame Case 1: count duplicates under a single DataFrame column Let’s start with a simple case, where you have the … Read more

4 Ways to Round Values in Pandas DataFrame

Here are 4 ways to round values in Pandas DataFrame: (1) Round to specific decimal places under a single DataFrame column Copy df[‘DataFrame column’].round(decimals = number of decimal places needed) (2) Round up values under a single DataFrame column Copy df[‘DataFrame column’].apply(np.ceil) (3) Round down values under a single DataFrame column Copy df[‘DataFrame column’].apply(np.floor) (4) … Read more

How to Load JSON String into Pandas DataFrame

To load a JSON string into Pandas DataFrame: Copy import pandas as pd df = pd.read_json(r’Path where the JSON file is stored\File Name.json’) print(df) Steps to Load JSON String into Pandas DataFrame Step 1: Prepare the JSON String To start with a simple example, let’s say that you have the following data about different products … Read more

Replace NaN Values with Zeros in Pandas DataFrame

Depending on the scenario, you may use either of the 4 approaches below in order to replace NaN values with zeros in Pandas DataFrame: (1) For a single column using fillna:  Copy df[‘DataFrame Column’] = df[‘DataFrame Column’].fillna(0) (2) For a single column using replace: Copy df[‘DataFrame Column’] = df[‘DataFrame Column’].replace(np.nan, 0) (3) For an entire … Read more

8 ways to apply LEFT, RIGHT, MID in Pandas

In this short guide, you’ll see how to extract specific characters within a string using Pandas. The goal, in each of the 8 scenarios below, is to extract only the digits within a string: (1) Extract the five digits from the left using str[:5]: Copy import pandas as pddata = {“Identifier”: [“55555-abc”, “77777-xyz”, “99999-mmm”]}df = … Read more