Examples of sort_values Isin used in Pandas Dataframe

Source: Internet
Author: User
1. In the dataframe of pandas, we often need to select a row for a specified condition based on a property, when the Isin method is particularly effective.

Import Pandas as Pddf = PD. DataFrame ([[1,2,3],[1,3,4],[2,4,3]],index = [' One ', ' both ', ' three '],columns = [' A ', ' B ', ' C ']) print df#        A  B  C # One    1  2  3#    1  3  4# three  2  4  3

Let's say we pick a row with a value of 1 in column A.

Mask = df[' A '].isin ([1]) #括号中必须为listprint mask# one       true#, true# three false# name:a    , Dtype:boolprint df[mask]#      A  B  C # one  1  2  3#  1  3  4

Dataframe in 2.pandas How to sort by the first keyword and the second keyword, where you can use Sort _values, the old version is Sort_index.

Import Pandas as Pddf = PD. DataFrame ([[[1,2,3],[2,3,4],[2,4,3],[1,3,7]],                  index = [' One ', ' one ', ' three ', ' four '],columns = [' A ', ' B ', ' C ']) Print df#        A  B  C # one    1  2  3#    2  3  4# three  2  4  3# Four   1  3  7df.sort_values (by=[' A ', ' B '],ascending=[0,1],inplace=true) print df#        A  B  C #    2  3  4# three  2  4 3# one    1  2 3# four   1  3  7

"Recommended"

1. How to use the sort () in Python

2. A detailed example tutorial using values () in Python

3. Share the usage example of sort in Python

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.