Cara memfilter pandas dataframe pada berbagai kondisi
Seringkali Anda mungkin ingin memfilter DataFrame pandas pada berbagai kondisi. Untungnya, hal ini mudah dilakukan dengan menggunakan operasi Boolean.
Tutorial ini memberikan beberapa contoh cara memfilter DataFrame panda berikut pada beberapa kondisi:
import pandas as pd #createDataFrame df = pd.DataFrame({'team': ['A', 'A', 'B', 'B', 'C'], 'points': [25, 12, 15, 14, 19], 'assists': [5, 7, 7, 9, 12], 'rebounds': [11, 8, 10, 6, 6]}) #view DataFrame df team points assists rebounds 0 to 25 5 11 1 to 12 7 8 2 B 15 7 10 3 B 14 9 6 4 C 19 12 6
Contoh 1: Filter pada beberapa kondisi menggunakan “Dan”
Kode berikut menunjukkan cara memfilter DataFrame menggunakan operator and ( & ):
#return only rows where points is greater than 13 and assists is greater than 7 df[(df. points > 13) & (df. assists > 7)] team points assists rebounds 3 B 14 9 6 4 C 19 12 6 #return only rows where team is 'A' and points is greater than or equal to 15 df[(df. team == 'A') & (df. points >= 15)] team points assists rebounds 0 to 25 5 11
Contoh 2: Filter pada beberapa kondisi menggunakan “Atau”
Kode berikut menunjukkan cara memfilter DataFrame menggunakan operator or ( | ):
#return only rows where points is greater than 13 or assists is greater than 7 df[(df. dots > 13) | (df. assists > 7)] team points assists rebounds 0 to 25 5 11 2 B 15 7 10 3 B 14 9 6 4 C 19 12 6 #return only rows where team is 'A' or points is greater than or equal to 15 df[( df.team == 'A') | (df. points >= 15)] team points assists rebounds 0 to 25 5 11 1 to 12 7 8 2 B 15 7 10 4 C 19 12 6
Contoh 3: Filter pada beberapa kondisi menggunakan daftar
Kode berikut menunjukkan cara memfilter DataFrame di mana nilai baris berada dalam daftar.
#define a list of values filter_list = [12, 14, 15] #return only rows where points is in the list of values df[df. points . isin (filter_list)] team points assists rebounds 1 to 12 7 8 2 B 15 7 10 3 B 14 9 6 #define another list of values filter_list2 = ['A', 'C'] #return only rows where team is in the list of values df[df. team . isin (filter_list2)] team points assists rebounds 0 to 25 5 11 1 to 12 7 8 4 C 19 12 6
Anda dapat menemukan tutorial panda lainnya di sini .