Python – Return NumPy Array From Function

Do you need to create a function that returns a NumPy array but you don’t know how? No worries, in sixty seconds, you’ll know! Go! πŸš€ A Python function can return any object such as a NumPy Array. To return an array, first create the array object within the function body, assign it to a … Read more

How to Find Min and Max Values in a 1D NumPy Array

Problem Formulation and Solution Overview This article will show you how to find the minimum and maximum values in a 1D NumPy array. To make it more interesting, these examples will generate an array of ten (10) random integers using the random.randint() function from the NumPy library and return the minimum and maximum values of … Read more

Python Print List Without Truncating

How to Print a List Without Truncating? Per default, Python doesn’t truncate lists when printing them to the shell, even if they are large. For example, you can call print(my_list) and see the full list even if the list has one thousand elements or more! Here’s an example: However, Python may squeeze the text (e.g., … Read more

How to Flatten a NumPy Array

Flatten to a 1D NumPy Array To flatten any NumPy array to a one-dimensional array, use the array.flatten() method that returns a new flattened 1D array. Here’s a simple example: Flatten NumPy Array Along Axis with reshape() To “flatten” a NumPy array along an axis, it’s often better to use the array.reshape() function. You can … Read more

Combine Images Using Numpy

Summary: You can combine images represented in the form of Numpy arrays using the concatenate function of the Numpy library as np.concatenate((numpydata_1, numpydata_2), axis=1). This combines the images horizontally. Use syntax: np.concatenate((numpydata_1, numpydata_2), axis=0) to combine the images vertically. Problem Formulation Consider you have two images represented as Numpy arrays of pixels. How will you … Read more

How to Get a Random Entry from a Python Dictionary

Problem Formulation and Solution Overview This article will show you how to get a random entry from a Dictionary in Python. To make it more interesting, we have the following running scenario: πŸ‘¨β€πŸ« The Plot: Mr. Sinclair, an 8th great Science Teacher, is giving his students a quiz on the first 25 Periodic Table elements. … Read more

How to Shuffle Two Arrays in Unison in Python?

Problem Formulation Given two arrays of the same length but different orders. How will you shuffle the two arrays in unison? Shuffling two arrays in unison means reordering the elements of both arrays in the same pattern. Let’s understand this with the help of an example. Example: Given:arr_1 = [[1, 1], [2, 2], [3, 3]]arr_2 … Read more

How to Find the Most Common Element in a NumPy Array

Problem Formulation and Solution Overview This article will show you how to find the most common element in a NumPy Array. To make it more interesting, we have the following running scenario: Carrie heard that Creative Prints is hiring a Python coder. They require the interviewee to answer several coding questions: one is to provide … Read more

Pandas – How to Find DataFrame Row Indices with NaN or Null Values

Problem Formulation and Solution Overview This article will show you how to find DataFrame row indices in Python with NaN or Null (empty) values. To make it more interesting, we have the following scenario: Rivers Clothing has given you a CSV file that requires a clean-up to make it usable for Payroll and Data Analysis. … Read more