Pandas-Merging/Joining

Merging two datasets is the process of bringing two datasets together into one and aligning the rows from each based on common attributes or columns.

Data Merging:

The pandas.merge() method joins two  data frames and align the rows from each other  by a “key” variable that contains unique values.

In pandas there are separate “Merge” and “Join” method but both do the similar work.

With pandas.merge(), you can only combine 2 data frames at a time. If you have more than 2 data frames to merge, you will have to use this method multiple times.

Let’s see  pandas.merge() and some of the available arguments to pass. Here is the general structure and the recommended bare minimum arguments to pass.

pandas.merge(left_data_frame, right_data_frame, on= , how= )

  • left is one of the data frames.
  • right is the other data frame
  • on is the variable, a.k.a the column, on which you want to merge on. This is the keyvariable and has to be the same name in both data frames.
  • If the data frames has different column names for the merge variables then you can also use left_on and right_on.
    • left_on is the variable name in the left data frame to be merged on.
    • right_on is the variable name in the left data frame to be merged on.

how is where you pass the options of merging. These include:

  • inner”, where only the observations with matching values based on the “on” argument that is passed are kept.
  • left”, where all observations will be kept from the data frame in the left argument regardless if there is matching values with the data frame in the right argument. observations that do not have a matching value based on the on argument in the “right” data frame will be discarded.
  • right”, where all observations will be kept from the data frame in the right argument regardless if there is matching values with the data frame in the left argument. Observations that do not have a matching value based on the on argument in the “left” data frame will be discarded.
  • outer”, all observations will be kept from both data frames.

Now let’s understand the concepts with example.

We are going to use following data set for operation.

  • user_usage.csv – This dataset containing users monthly mobile usage details.
  • user_device.csv – This data set is containing details of an individual “use” of the system, with dates and device information.
  • android_devices.csv – This dataset with device and manufacturer data, which lists all Android devices and their model code.

Let’s understand the concept with coding..