Merging Python Datasets: Merge and Join__python

Source: Internet
Author: User

The following will be transferred from the College, more merge operations and the use of join methods, you can directly search the original reading

To introduce the "merge" approach to DataSet Processing: Merge and join, and to better demonstrate the relevant operations, you need to do some preparation, including importing the required pandas libraries and numpy libraries, and building display classes that are easy to display:

Import pandas as PD
import NumPy as NP

class display (object): "" "
    display HTML representation of multiple objec TS "" ""
    template = "" "<div style=" float:left;padding:10px; " >
    <p style= ' font-family: "Courier New", Courier, Monospace ' >{0}</p>{1}
    </div> "
    " " def __init__ (self, *args):
        Self.args = args

    def _repr_html_ (self): return
        ' \ n '. Join (Self.template.format (A, eval (a). _repr_html_ ())
                         For a in Self.args

    def __repr__ (self): return
        ' \ n '. Join (a + ' \ n ' + repr (eval (a)) for
                           A in Self.args)

Class display allows us to display multiple results of output in one line.

Next, we construct the instance data df1 and DF2, which record the employee's group and the employee's date of employment respectively:

DF1 = PD. Dataframe ({' Employee ': [' Bob ', ' Jake ', ' Lisa ', ' Sue '],
                    ' group ': [' Accounting ', ' Engineering ', ' Engineering ',
                              ' HR '})
DF2 = PD. Dataframe ({' Employee ': [' Lisa ', ' Bob ', ' Jake ', ' Sue '],
                    ' hire_date ': [2008, 2014]})
display (' Df1 ', ' Df2 ')

Using the Pandas Library's merge function can help us to merge data, and we can see that in the merged data frame DF3 includes the employee's corresponding group and employment date information:

DF3 = Pd.merge (df1, DF2)
df3

Similarly, we can use this function to incorporate more information, such as the supervisory leadership of each employee:

DF4 = PD. Dataframe ({' Group ': [' Accounting ', ' Engineering ', ' HR '],
                    ' supervisor ': [' Carly ', ' Guido ', ' Steve ']})
display (' Df3 ', ' df4 ', ' Pd.merge (df3, DF4) ')

The merge of functions will default to match the same column names in the two original datasets, and when the sample size of the two datasets is different, the merged data boxes will automatically expand:

DF5 = PD. Dataframe ({' Group ': [' Accounting ', ' Accounting ',
                              ' Engineering ', ' Engineering ', ' hr ', ' hr '],
                    ' skills ': [' Math ', ' spreadsheets ', ' coding ', ' Linux ',
                               ' spreadsheets ', ' organization '})
display (' Df1 ', ' Df5 ', "Pd.merge" ( DF1, Df5) ")

Of course, we can also specify the primary key for dataset merging through parameter on:

Display (' Df1 ', ' df2 ', "Pd.merge (DF1, DF2, on= ' employee ')")

At the same time, we can specify the column names through parameter left_on and right_on, so that different column names correspond to each other, and then merge:

DF3 = PD. Dataframe ({' Name ': [' Bob ', ' Jake ', ' Lisa ', ' Sue '], '
                    salary ': [70000, 80000, 120000, 90000]})
display (' Df1 ', ' Df3 ', ' Pd.merge (DF1, DF3, left_on= "employee", right_on= "name")

We can then use the drop command to shed the repeating columns in the data box to optimize the results of the data merge:

Pd.merge (DF1, DF3, left_on= "employee", right_on= "name"). Drop (' name ', Axis=1)

The above content is transferred from the number Analysis College, the more merge operation and the Join method use, may search directly to read the original text

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.