Python pandas. Dataframe the best way to select and modify data. Loc,.iloc,.ix

Source: Internet
Author: User

Let's create a data frame by hand.

[Python]View PlainCopy
    1. Import NumPy as NP
    2. Import Pandas as PD
    3. DF = PD. DataFrame (Np.arange (0,2). Reshape (3), columns=list (' abc ' )

DF is such a drop

So how do you choose the three ways to pick the data?

One, when each column already has column name, with DF [' a '] can choose to take out a whole column of data. If you know column names and index, and both are well-entered, you can choose. loc

[Python]View PlainCopy
    1. df.loc[0, ' a ']
    2. df.loc[0:3, [' a ', ' B ']
    3. df.loc[[1, 5], [' B ', ' C ']]

Because this side we did not name index, so is dataframe automatically assigned, for the number 0-9

Second, if we suspect that column name is too long, input inconvenient, or index is a column of time series, more bad input, then you can choose. Iloc. This side I I think the representative index, better to remember points.

[Python]View PlainCopy
    1. df.iloc[1,1]
    2. df.iloc[0:3, [0,1]]
    3. df.iloc[[0, 3, 5], 0:2]

Iloc allows us to select the data using the slice (slice) method of column.

Third,. IX features are more powerful, it allows us to mix the subscript and the name of the selection. It can be said that it covers all the previous uses. Basically change the front to Df.ix can be successful, but one thing is that

Df.ix [[.. 1 ...], [.. 2.]], the 1 box must be unified, must also be subscript or name, 2 box is also the same. BTW, the 1 box is used to specify that the row,2 box is the specified column, and all of the above methods are, of course, the rule.

This is my current understanding.

Python pandas. Dataframe the best way to select and modify data. Loc,.iloc,.ix

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.