Connection and append __python of Python dataset processing

Source: Internet
Author: User

The following is transferred from the analysis of the college, the original text follow-up and on the index value in the process of merging, students need to learn to directly see the original

To introduce you to the connection (CONCAT) and append (append) in the dataset merge method, first do some preparation work:

1. Import Pandas Library and NumPy library:

Import pandas as PD
import NumPy as NP

2. Define a MAKE_DF function to generate the sample data box:

def MAKE_DF (COLS, Ind): ""
    Quickly Make a Dataframe "" "
    data = {c: [str (c) + str (i) for I in IND] for
            C in cols Return to
    PD. Dataframe (data, Ind)

# Sample Box
make_df (' ABC ', Range (3))

3. Define a display class to allow us to more clearly display the results of the output:

class display (object): "" "
    display HTML representation of multiple Objects" "
    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)

Let's get down to business.

Before we talked about some methods of array concatenation, such as using the CONCATENATE function in NumPy, we could concatenate multiple lists into an array:

x = [1, 2, 3]
y = [4, 5, 6]
z = [7, 8, 9]
np.concatenate ([x, Y, z])

Array ([1, 2, 3, 4, 5, 6, 7, 8, 9])

At the same time, for a multi-level list, we can also connect to a specified dimension by adjusting the value of the axis parameter:

x = [[1, 2],
     [3, 4]]
np.concatenate ([x, X], Axis=1)

Array ([[1, 2, 1, 2],
[3, 4, 3, 4]]

Similarly, for the pandas sequence, the Pandas library also provides a join function, and we can use the concat function in pandas to connect two sequences:

Ser1 = PD. Series ([' A ', ' B ', ' C '], index=[1, 2, 3])
Ser2 = PD. Series ([' D ', ' E ', ' F '], index=[4, 5, 6])
pd.concat ([Ser1, Ser2])

Next, we use the previously defined data box generation function to build several sample data boxes to see how the connection function concat the data box:

DF1 = MAKE_DF (' ab ', [1, 2])
DF2 = MAKE_DF (' AB ', [3, 4])
display (' Df1 ', ' df2 ', ' pd.concat ' ([Df1, DF2])

Using the Display class we defined earlier, we also displayed two data boxes and their connected results on one line. As you can see, the CONCAT function by default connects the second data box to the bottom of the first Data box.
Sometimes, we may need to make a horizontal connection to two data boxes, as shown below, we can do this by adjusting the parameters axis of the concat function:

DF3 = MAKE_DF (' AB ', [0, 1])
DF4 = MAKE_DF (' CD ', [0, 1])
display (' Df3 ', ' df4 ', "Pd.concat ' ([Df3, Df4], axis= ' col ')")

The above content is transferred from the number Analysis College, the original text follow-up and on the index value in the process of merging, students need to learn to directly see the original

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.