Check if a File or Directory Exists using Python

You can use the following templates to check if a file or a directory exists using Python: (1) Check if a file exists using os.path.isfile: Copy import os.pathfile_exists = os.path.isfile(r”path where the file is stored\file_name.file_extension”)print(file_exists) (2) Check if a directory exists using os.path.isdir: Copy import os.pathdirectory_exists = os.path.isdir(r”path of directory”)print(directory_exists) Steps to Check if a … Read more

Count NaN values in Pandas DataFrame

To count NaN values in Pandas DataFrame: (1) Under a single DataFrame column: Copy df[“column name”].isna().sum() (2) Under an entire DataFrame: Copy df.isna().sum().sum() (3) Across a single DataFrame row: Copy df.loc[[index value]].isna().sum().sum() The Example Suppose you created the following DataFrame that contains NaN values: Copy import pandas as pdimport numpy as npdata = { “first_set”: … Read more

Drop Columns with NaN Values in Pandas DataFrame

Here are 2 ways to drop columns with NaN values in Pandas DataFrame: (1) Drop any column that contains at least one NaN: Copy df.dropna(axis=’columns’, inplace=True) (2) Drop column/s where ALL the values are NaN: Copy df.dropna(axis=’columns’, how=’all’, inplace=True) The Example For demonstration purposes, let’s create a DataFrame with 5 columns, where: Here is the … Read more

Convert Floats to Strings in Pandas DataFrame

Here are 3 approaches to convert floats to strings in Pandas DataFrame for: (1) A single DataFrame column using astype(str): Copy df[“DataFrame Column”] = df[“DataFrame Column”].astype(str) (2) A single DataFrame column using apply(str): Copy df[“DataFrame Column”] = df[“DataFrame Column”].apply(str) (3) An entire DataFrame using: Copy df = df.astype(str) 3 Approaches to Convert Floats to Strings … Read more

Convert Floats to Integers in Pandas DataFrame

You can convert floats to integers in Pandas DataFrame using: (1) astype(int): Copy df[“DataFrame Column”] = df[“DataFrame Column”].astype(int) (2) apply(int): Copy df[“DataFrame Column”] = df[“DataFrame Column”].apply(int) In this guide, you’ll see 4 scenarios of converting floats to integers for: 4 Scenarios of Converting Floats to Integers in Pandas DataFrame (1) Convert floats to integers for … Read more

Convert Pandas DataFrame to a Dictionary

To convert Pandas DataFrame to a dictionary: Copy my_dictionary = df.to_dict() Steps to Convert Pandas DataFrame to a Dictionary Step 1: Create a DataFrame For example, create a DataFrame with two columns: Copy import pandas as pddata = { “Product”: [“Laptop”, “Printer”, “Monitor”, “Tablet”], “Price”: [1200, 100, 300, 150],}df = pd.DataFrame(data)print(df)print(type(df)) You’ll then get the … Read more

How to Convert Pandas DataFrame to Series

To convert Pandas DataFrame to Series using squeeze: Copy df.squeeze() In this guide, you’ll see 3 scenarios of converting: (1) Convert a Single DataFrame Column into a Series To start with a simple example, let’s create a DataFrame with a single column: Copy import pandas as pd data = {“Products”: [“Computer”, “Printer”, “Tablet”, “Chair”, “Desk”]} … Read more

Count the Number of Elements in a Python List

The len() function can be used to count the number of elements in a Python list: Copy len(my_list) 3 Examples of Counting the Number of Elements in a List Example 1: List that Contains Strings To start with a simple example, create a list that contains 5 names: Copy names_list = [“Jeff”, “Ben”, “Maria”, “Sophia”, … Read more

Create Horizontal Bar Chart using Matplotlib

Here is a simple template to create a horizontal bar chart using Matplotlib: Copy import matplotlib.pyplot as plty_axis = [“value_1”, “value_2”, “value_3”, …]x_axis = [“value_1”, “value_2”, “value_3”, …]plt.barh(y_axis, x_axis)plt.title(“title name”)plt.ylabel(“y axis name”)plt.xlabel(“x axis name”)plt.show() Steps to Create Horizontal Bar Chart using Matplotlib Step 1: Gather the data for the chart For example, let’s use the … Read more

How to Change the Order of Columns in Pandas DataFrame

In this short guide, you’ll see how to change the order of columns in Pandas DataFrame. Here are the steps: Steps to Change the Order of Columns in Pandas DataFrame Step 1: Prepare the Data for the DataFrame To begin, prepare the data for your DataFrame. For example: Name Age Profession Country Jack 22 Accountant … Read more