Python

How to Get the File Size using Python

You can use the syntax below in order to get the file size (in bytes) using Python: import os.path file_path = r’path where the file is stored\file name.file extension’ file_size = os.path.getsize(file_path) print(file_size) Optionally, you can use the following code to get the size in bytes, kilobytes, megabytes and gigabytes: import os.path file_path = r’path …

How to Get the File Size using Python Read More »

How to Extract the File Extension using Python

Here are 3 ways to extract the file extension using Python: (1) Extract the file extension with the dot: import os.path my_path = r’path where the file is stored\file name.file extension’ ext = os.path.splitext(my_path)[1] print(ext) (2) Extract the file extension without the dot: import os.path my_path = r’path where the file is stored\file name.file extension’ …

How to Extract the File Extension using Python Read More »

Check if a File or Directory Exists using Python

You can use the following templates to check if a file or directory exists using Python: (1) Check if a file exists using os.path.isfile: import os.path file_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: import os.path directory_exists = os.path.isdir(r’path of directory’) print(directory_exists) Steps to …

Check if a File or Directory Exists using Python Read More »

How to Count NaN values in Pandas DataFrame

You can use the following syntax to count NaN values in Pandas DataFrame: (1) Count NaN values under a single DataFrame column: df[‘column name’].isna().sum() (2) Count NaN values under an entire DataFrame: df.isna().sum().sum() (3) Count NaN values across a single DataFrame row: df.loc[[index value]].isna().sum().sum() Let’s see how to apply each of the above cases using …

How to Count NaN values in Pandas DataFrame Read More »

How to Convert Floats to Strings in Pandas DataFrame

In this short guide, you’ll see 3 approaches to convert floats to strings in Pandas DataFrame for: (1) An individual DataFrame column using astype(str): df[‘DataFrame Column’] = df[‘DataFrame Column’].astype(str) (2) An individual DataFrame column using apply(str): df[‘DataFrame Column’] = df[‘DataFrame Column’].apply(str) (3) An entire DataFrame using: df = df.astype(str) Next, you’ll see how to apply …

How to Convert Floats to Strings in Pandas DataFrame Read More »