Pandas Merging multiple dataframe (MERGE,CONCAT)

Source: Internet
Author: User

At the time of data processing, especially in the big data contest, often encounter a problem is that multiple forms of merging problems, such as a form has user_id and age two fields, another form has user_id and sex two fields, to merge these two tables into only user_id, Age, sex three fields of the table what to do, the ordinary stitching is not possible, because user_id each row is not the corresponding, like the building blocks of horizontal stitching is certainly not. There is a merge function in pandas to do this practical function, the word "merge" will be a bit of SQL language should not be unfamiliar.


Let's talk about the merge function:

DF = Pd.merge (DF1, DF2, how= ' left ', on= ' user_id ')

Usage is very simple, say the latter two parameters, how= "" parameter indicates which table key, above the how= "left" to indicate the table DF1, and key is on= "" parameter

How= "left" that is, keep the user_id field of all information, not increase or decrease, but stitching only the DF2 table and df1 in the user_id field of the intersection of the part of the merge on it, if there is df2 in the user_id does not appear in DF1, Just discard the sample and leave it out of the process.

If you are merging multiple keys:

DF = Pd.merge (DF1, DF2, how= ' left ', on=[' user_id ', ' sku_id ')


But if you want to simply "stitch" instead of merging, use the concat function :

DF = Pd.concat ([Df_user, Dummies_sex, Dummies_age, Dummies_level], Axis=1)

This preserves all the information of these forms, the parameter Axis=1 represents the column concatenation, and the axis=0 represents the row stitching.

To ensure that the number of rows on the back of the form is the same, and each row of the corresponding key is the same, column stitching becomes meaningful



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.