Convert Strings to Integers in Pandas DataFrame

Here are 3 approaches to convert strings to integers in Pandas DataFrame: (1) The astype(int) approach: Copy df[“dataframe_column”] = df[“dataframe_column”].astype(int) (2) The apply(int) approach: Copy df[“dataframe_column”] = df[“dataframe_column”].apply(int) (2) The map(int) approach: Copy df[“dataframe_column”] = df[“dataframe_column”].map(int) Let’s review an example with the steps to convert strings to integers. Steps to Convert Strings to Integers in … Read more

Join Pandas DataFrames using Merge

To join Pandas DataFrames using merge: Copy pd.merge(df1, df2, how=’type of join’, on=[‘df1 key’, ‘df2 key’]) In this short guide, you’ll see the steps create the following joins: Steps to Join Pandas DataFrames using Merge Step 1: Create the DataFrames to be joined Let’s say that you have two datasets that you’d like to join: … Read more

Compare Values between two Pandas DataFrames

In this short guide, you’ll see how to compare values between two Pandas DataFrames. The Steps Step 1: Create two DataFrames To start, create the two DataFrames below. Note that your two DataFrames must have the same number of rows before making the comparison. Copy import pandas as pd# Create the first DataFramedata_1 = { … Read more

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

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