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 to 1-D ones. Below is the example. Output: 2-D array: [[ 5 2 3 4 … Read more

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 is expecting to have 1-D numpy arrays or lists, since it is how columns within … Read more

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 Combining Arrays into Dataframe Example for Method 1: In the following, we create two arrays, … Read more

3 Examples for Numpy in Python Numpy generates the product of two arrays. Specifically, for, 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: If either a or b is 0-D (scalar), it is equivalent to multiply. Using np.multiply(a, b) or a * b is preferred. Example 1: … Read more

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 in the second row]] Method 2: np.zeros(shape(row number, column number)) Method 3: array_name=np.arange(length number)array_name=array_name.reshape(row number, … Read more

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 number Output: False Since it is not a scalar, we can check whether it is … Read more

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 to create an array Output: [‘City1’ ‘City1’ ‘City1’ ‘City1’ ‘City1’ ‘City2’ ‘City2’ ‘City2’ ‘City2’ ‘City2’] … Read more

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 requires the user to manually set all the values in the array, and thus you … Read more

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] Method 1: np.tolist() list_1: [5, 8, 4, 4] Method 2: list() We can use Python … Read more

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 a sample dataframe The following is to generate a dataframe and then print it out. … Read more