Category: NumPy
How to Convert 2-D Arrays to 1-D Arrays
There are 3 methods to convert 2-D arrays to 1-D ones in Numpy. The following shows the key syntax. Method 1: numpy.ravel() Method 2: ndarray.flatten(order=’C’) Method 3: ndarray.reshape(-1) Example 1 (Method 1): We can use numpy.ravel() to convert 2-D arrays...
Read Full Article →
How to Fix: Data must be 1-dimensional
You might encounter the following error when trying to convert Numpy arrays to a pandas dataframe. Exception: Data must be 1-dimensional 1. Reproduce the Error Output: Exception: Data must be 1-dimensional 2. Why the Error Happens It happens because pd.DataFrame...
Read Full Article →
How to Combine Multiple Numpy Arrays into a Dataframe
This tutorial will show how you can combine multiple arrays (e.g., 2 arrays of X and Y) into a Pandas dataframe. The following summarizes the two methods. Method 1: pd.DataFrame ({‘X’:X,’Y’:Y}) Method 2: combined_array=np.column_stack((X,Y))pd.DataFrame(combined_array, columns = [‘X’,’Y’]) Two Examples of...
Read Full Article →
3 Examples for Numpy np.dot()
Np.dot() in Python Numpy generates the product of two arrays. Specifically, for np.dot(a, b), Situation 1: If both a and b are 1-D arrays, it is inner product of vectors. Situation 2: If both a and b are 2-D arrays, it is matrix multiplication. Using matmul or a @ b is preferred. Situation 3:...
Read Full Article →
Examples of Generating 2-D Numpy Array
This tutorial provides 3 methods to create 2-D Numpy Arrays in Python. The following is the key syntax for these 3 methods. After that, 3 Python Numpy code examples are provided. Method 1: np.array[[numbers in the first row ], [numbers...
Read Full Article →
Numpy Checks it is a Scalar or an Array (Examples)
You can use the np.isscalar() to check whether a variable is a scalar or array. The following shows Python code examples checking it is a scalar or array. Example 1: A number Output: True Example 2: An array with one...
Read Full Article →
How to Convert Array to Column in Pandas dataframe
You can use pd.DataFrame() function to convert an array to a column in a Pandas dataframe. The following shows examples of how to convert array from Numpy to a column in Pandas. Example 1: Single Column Step 1: Using Numpy...
Read Full Article →
How to Create an Empty Matrix in NumPy
The syntax of creating an empty array is as follows. Method 1: Full of zero numpy.zeros(shape=(n,m)) Method 2: Really empty Note that, empty, unlike zeros, does not set the array values to zero, and may therefore be marginally faster. However, it...
Read Full Article →
How to Convert NumPy Array to List
We can use tolist() and list() to convert an array into a list in Python. The following is the syntax. Method 1: array.tolist() Method 2: list(array) The following is to generate an array using Numpy. Array_1: [5 8 4 4]...
Read Full Article →
How to Add Numpy Arrays to a Pandas DataFrame
You can add a NumPy array as a new column to Pandas dataframes by using the tolist() function. The following are the syntax statement as well as examples showing how to actually do it. df['new_column_name'] = array_name.tolist() Step 1: Generate...
Read Full Article →