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

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

Outer Join in Pandas

Outer Join returns all records from both the left or right dataframes. When rows in one dataframe do not match another dataframe, the joined dataframe will have NaN for cells of the unmatched rows. We can use how=’outer’ in join() to outer join two dataframes in Pandas. The basic syntax is as follows, in which df_1 … 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

How to Save Pandas Dataframe as csv file

To save Pandas dataframe as CSV file, you can use the function of df.to_csv. The following shows the steps. df.to_csv(“file_name.csv”) Step 1: Dataframe example The following Python code is to generate the sample dataframe. The following is the print out of the generated sample dataframe. df_1: Brand Location a Tesla CA b Toyota CA c … Read more

Pandas Left Join Two Dataframes

Introduction There are two methods to left join two dataframes in Pandas. Method 1 We can use how=’left’ tells join() to left join two dataframes. df_1.join(df_2, how=’left’) Method 2 We can use how=’left’ tells merge() to left join two dataframes. df_1.merge(df_2, how=’left’, left_index=True,right_index=True) Sample Data The following is the two dataframes to be left joined. … Read more

Merge Two Dataframes in Pandas based on a Column

Method 1 Use merge() to merge two dataframes in Pandas based on a column. When the column name is the same in both dataframes, we can use the following statement to merge two dataframes based on that column. col_name is the name of the common column in df_1 and df_2. df_1.merge(df_2, on=”col_name”) The following is … Read more

Join Two Dataframes in Pandas based on a Column

There are two methods to join two dataframes in Pandas based on a column. (Note that, by default, join() will use index to combine two dataframes.) Method 1: We can use a combination of set_index(‘col_name’) and on=’col_name’ for df_2. col_name is a common column in both df_1 and df_2. df_1.join(df_2.set_index(‘col_name’),on=’col_name’) Method 2: We can use … Read more

Pandas: Append Two Dataframes

append() adds rows of other to the end of caller, returning a new object. The following is the basic syntax of append(), in which df_1 is the main one, and df_2 will be appended into df_1. df_1.append(df_2) The following is the dataframes that will be used later in the examples. df_1: Brand Location a Tesla CA b … Read more