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