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 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: 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 Create Dummy Variable in Python

This tutorial shows two methods of creating dummy variables in Python. The following shows the key syntax. Method 1: Use Numpy.where() to create a dummy variable np.where(df[‘column_of_interest’] == ‘value’ ,1,0) Method 2: Use apply() and lambda function to create a dummy variable df[‘column_of_interest’].apply(lambda x: 1 if x==’value’ else 0) Example 1: Use numpy.where() to create … Read more

How to Use numpy.random.seed()

numpy.random.seed() provides a seed, which acts as a starting point number generator algorithm. For the same seed, we will always get the same set of random numbers on any machine. If you prefer to have different sets of random numbers every time you run the code, do not set the seed. In contrast, if you … Read more

Calculate Means Group by Two Columns in Pandas (3 Examples)

The following provides 3 different methods of calculating means group by two Columns in Python. Method 1: df.groupby([“column_1″,”column_2”]).mean() Method 2: df.groupby([“column_1″,”column_2”]).agg(‘mean’) Method 3: pd.crosstab(index=df[‘column_1’], columns=df[‘column_2’],values=df[‘dv’],aggfunc=’mean’) Prepare the data Output: city store sales 0 City1 store1 10 1 City1 store2 20 2 City1 store1 20 3 City1 store2 50 4 City1 store1 30 5 City2 store2 10 … Read more

Outer Merge in Pandas

Introduction Outer Merge returns all records from both the left or right dataframes. When rows in one dataframe do not match another dataframe, the merged dataframe will have NaN for the cells. We can use how=’outer’ in merge() to outer merge two dataframes in Pandas. The basic syntax is as follows, in which df_1 and df_2 … Read more

Left Merge in Pandas Python

We can use how=’left’ tells merge() to left merge two dataframes. The following is the Pandas syntax, in which df_1 and df_2 are two dataframes to be merged. df_1.merge(df_2, how=’left’, left_index=True, right_index=True) Step 1: Prepare the data to be left merged The following is the two dataframes to be left merged. df_1: Brand Location a … Read more

Convert a Pandas DataFrame to a Dictionary

df.to_dict() can be used to convert a Pandas dataframe to a dictonary. The basic syntax is as follows. Note that,’list’ does not return index. df.to_list( orient = ‘list’ ) Sample Data Brand Location a Tesla CA b Toyota CA c Tesla NY d Ford MA Method 1: orient=’list’ Below is the example of using orient=’list’ … Read more