Convert Python List to a NumPy Array

The following syntax can be used to convert a Python list to a numpy array: my_array = np.array(my_list) In this guide, you’ll see how to convert: Python list to a numpy array List of lists (multi-dimensional list) to a numpy array (1) Convert Python List to a NumPy Array Let’s create a simple list with … Read more

How to Convert NumPy Array to a List in Python

You can use tolist() in order to convert a numpy array to a list in Python: my_list = my_array.tolist() Below are the steps to convert a numpy array to a list (as well as to a list of lists) using practical examples. Steps to Convert NumPy Array to a List in Python Step 1: Create … Read more

How to Sort Pandas Series (examples included)

Here is the syntax to sort Pandas Series: (1) Sort Pandas Series in an ascending order: sortedSeries = mySeries.sort_values(ascending=True) (2) Sort Pandas Series in a descending order. In this case, simply set ascending=False: sortedSeries = mySeries.sort_values(ascending=False) In this guide, you’ll see how to sort Pandas Series that contains: String/text values Numeric values NaN values Sort … Read more

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 … 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’ … Read more

How to 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: 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 get … Read more

How to get the Latest File in a Folder using Python

In this guide, you’ll see how to get the latest file in a folder using Python. To start, here is a template that you may use to get the latest file: import glob import os.path folder_path = r’path where your files are located’ file_type = ‘\*type’ files = glob.glob(folder_path + file_type) max_file = max(files, key=os.path.getctime) … Read more

How to Convert JSON String to TEXT File using Python

The following template can be used to convert a JSON string to a text file using Python: import pandas as pd df = 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) Next, you’ll see the steps to apply the … Read more

How to Get the Modified Time of a File using Python

You can use the following template to get the modified time of a file using Python: import os.path modified_time = os.path.getmtime(r’path where the file is stored\file name.file extension’) print(modified_time) The above approach expresses the time in seconds since the epoch. If you wish to convert the time to a string that represents a local time, … 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 … Read more