Convert Python List to NumPy Array

To convert a Python list to a NumPy array: Copy my_array = np.array(my_list) In this guide, you’ll see how to convert: Case 1: Convert Python List to a NumPy Array To start, create a simple list with 6 elements: Copy my_list = [10, 15, 20, 25, 30, 35]print(my_list)print(type(my_list)) This is how the list would look … Read more

Sort Pandas Series

Here is the syntax to sort Pandas Series: (1) Sort Pandas Series in an ascending order: Copy ser.sort_values(ascending=True, inplace=True) (2) Sort Pandas Series in a descending order. In this case, simply set ascending=False: Copy ser.sort_values(ascending=False, inplace=True) In this guide, you’ll see how to sort Pandas Series that contains: Sort Pandas Series that Contains String/Text Values … Read more

How to Get the File Size using Python

To get the file size (in bytes) using Python: Copy import os file_path = r”path where the file is stored\file_name.file_extension” file_size = os.path.getsize(file_path) print(file_size) Optionally, to get the size in bytes, kilobytes, megabytes and gigabytes: Copy import os file_path = r”path where the file is stored\file_name.file_extension” file_size = os.path.getsize(file_path) print(“File size in Bytes: ” + … 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: Copy import os.path file_path = r”path where the file is stored\file_name.file_extension” file_extension = os.path.splitext(file_path)[1] print(file_extension) (2) Extract the file extension without the dot: Copy import os.path file_path = r”path where the file is stored\file_name.file_extension” file_extension = … Read more

Get the first N rows in Pandas DataFrame

You can use df.head() to get the first N rows in Pandas DataFrame. For example, if you need the first 4 rows, then use: Copy df.head(4) Alternatively, you can specify a negative number within the brackets to get all the rows, excluding the last N rows. For example, you can use the following syntax to … Read more

Get the Latest File in a Folder using Python

To get the latest file in a folder using Python (where “type” represents the file type, such as csv, or txt): Copy import globimport os.pathfolder_path = r”path where your files are located”file_type = r”\*type”files = glob.glob(folder_path + file_type)max_file = max(files, key=os.path.getctime)print(max_file) Steps to get the Latest File in a Folder using Python Step 1: Capture … Read more

Convert JSON to TEXT File using Python

The following template can be used to convert a JSON file to a text file using Python: Copy import pandas as pddf = pd.read_json(r”Path where the JSON file is saved\File Name.json”)df.to_csv(r”Path where the new TEXT file will be stored\New File Name.txt”, index=False) Steps to Convert JSON to TEXT using Python Step 1: Prepare the JSON … Read more

Get the Modified Time of a File using Python

To get the modified time of a file using Python: Copy import os.path import time # Specify the full file path file_path = r”Path where the file is stored\file_name.file_extension” # Get the modified time of the file modified_time = os.path.getmtime(file_path) # Convert the modified time to a readable format formatted_time = time.strftime(“%Y-%m-%d %H:%M:%S”, time.localtime(modified_time)) print(formatted_time) … Read more

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