How to Union Pandas DataFrames using Concat

You can union Pandas DataFrames using concat: Copy pd.concat([df1, df2]) You may concatenate additional DataFrames by adding them within the brackets. Steps to Union Pandas DataFrames using Concat Step 1: Create the first DataFrame For example, let’s say that you have the following DataFrame about products: Copy import pandas as pd data1 = { “product”: … Read more

How to Select Rows from Pandas DataFrame

In this short guide, you’ll see the steps to select rows from Pandas DataFrame based on the conditions specified. Steps to Select Rows from Pandas DataFrame Step 1: Gather your data Firstly, you’ll need to gather your data. Here is an example of a data gathered about boxes: Color Shape Price Green Rectangle 10 Green … Read more

Example of K-Means Clustering in Python

K-Means Clustering is a concept that falls under Unsupervised Learning. This algorithm can be used to find groups within unlabeled data. Topics to be covered: Example of K-Means Clustering in Python To start, here is an example of a two-dimensional dataset: Copy import pandas as pddata = { “x”: [25, 34, 22, 27, 33, 33, 31, … Read more

How to Remove Duplicates in Pandas DataFrame

To remove duplicates across the entire DataFrame: Copy df.drop_duplicates() To remove duplicates under a single DataFrame column: Copy df.drop_duplicates(subset=[“column_name”]) Steps to Remove Duplicates in Pandas DataFrame Step 1: Gather the data that contains the duplicates Firstly, you’ll need to gather the data that contains the duplicates. Here is an example of data that contains duplicates: … Read more

Export Pandas DataFrame to JSON File

To export Pandas DataFrame to a JSON file: Copy df.to_json(r”Path to store the exported JSON file\File Name.json”) Steps Step 1: Create a DataFrame Imagine that you have the following DataFrame that contains data about products and prices: Copy import pandas as pddata = { “Product”: [“Computer”, “Printer”, “Monitor”, “Tablet”, “Keyboard”], “Price”: [1200, 200, 500, 350, … Read more

Concatenate Lists in Python

You can use the plus operator (‘+’) in order to concatenate lists in Python: Copy concatenated_list = list_1 + list_2 For example, suppose that you want to concatenate the following two lists: Copy products_1 = [“computer”, “tablet”, “printer”, “monitor”]products_2 = [“keyboard”, “mouse”, “laptop”, “scanner”] You can then use the + operator to concatenate the two … Read more

How to add Python to Windows PATH

There are few ways to add Python to Windows PATH. In this short guide, you’ll see two methods to add Python to Windows path: Method 1: Install a Recent Version of Python You can easily add Python to Windows path by downloading a recent version of Python, and then checking the box to Add Python … Read more

Create Pivot Table in Python using Pandas

In this short guide, you’ll see how to create a pivot table in Python using Pandas. The Example Imagine that you have the following DataFrame that contains data about sales across 4 quarters: Copy import pandas as pddata = {‘person’: [‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘A’, ‘B’, ‘C’, ‘D’, ‘E’, … Read more

IF, ELIF and ELSE in Python (with Examples)

Here is a template that you may reference when performing IF, ELIF and ELSE in Python: Copy if condition_1: perform an action if condition_1 is metelif condition_2: perform an action if condition_2 is metelse: perform an action if neither condition_1 nor condition_2 is met Next, you’ll see the following 4 examples: Example 1: IF and … Read more