[Python] Pandas's sort_values isin use skills __python

Source: Internet
Author: User

1. In the dataframe of pandas, we often need to select the rows of a specified condition based on a property, at which point the Isin method is particularly effective.

Import pandas as PD
DF = PD. Dataframe ([[1,2,3],[1,3,4],[2,4,3]],index = [' One ', ' two ', ' three '],columns = [' A ', ' B ', ' C '])
print DF
#        A  B  C
# One    1  2  3
# two    1  3  4
# three  2  4 3

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

Mask = df[' A '].isin ([1]) #括号中必须为list
print Mask
# one       true
# two       True
# three    False
# name:a, dtype:bool
print Df[mask]
#      A  B  C
# one  1  2  3
# Two  1  3  4


The dataframe in 2.pandas how to sort by the first keyword, the second keyword, here you can use Sort_values, the old version of Sort_index.

Import pandas as PD
DF = PD. Dataframe ([[1,2,3],[2,3,4],[2,4,3],[1,3,7]],
                  index = [' One ', ' two ', ' three ', ' four '],columns = [' A ', ' B ', ' C ']]
print DF
#        A  B  C
# one    1  2  3
# two    2  3  4
# Three  2  4  3
# four   1  3  7
df.sort_values (by=[' A ', ' B '],ascending=[0,1],inplace =true)
print DF
#        A  B  C
# two    2  3  4
# three  2  4  3
# One    1  2  3
# four   1  3  7



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.