xxxxxxxxxx
In [96]: df
Out[96]:
A B C D
a 1 4 9 1
b 4 5 0 2
c 5 5 1 0
d 1 3 9 6
In [99]: df[(df.A == 1) & (df.D == 6)]
Out[99]:
A B C D
d 1 3 9 6
xxxxxxxxxx
# Way 1 (using isin method)
df[df['col'].isin(['val1','val2'])]
# Way 2 (logical operation)
df[(df['col1'] > val1) & (df['col2'] < val2)]
# Way 3 (using loc keyword)
df.loc[index_list, col_list]
# Way 4 (using list comprehension of a series from a dataframe)
['X' if val=='some_val' else 'Y' for val in df['col']]
# Way 5
df.query('col1=="A" or (col2=="B" and col3 < 90)')
# Not exacly filtering, more like subsetting/slicing with iloc keyword
df.iloc[1:3, 0:2]
xxxxxxxxxx
#Filtering for SP state and price up or equal 115
sp_above_mean = df[(df['price'] >= 115) & (df['seller_state'] == 'SP')]
xxxxxxxxxx
1
2
3
4
# filter rows for year 2002 using the boolean expression
>gapminder_2002 = gapminder[gapminder['year']==2002]
>print(gapminder_2002.shape)
(142, 6)
xxxxxxxxxx
df = pd.DataFrame(np.array(([1, 2, 3], [4, 5, 6])),
index=['mouse', 'rabbit'],
columns=['one', 'two', 'three'])
>>> df
one two three
mouse 1 2 3
rabbit 4 5 6
# select columns by name
df.filter(items=['one', 'three'])
one three
mouse 1 3
rabbit 4 6
# select columns by regular expression
df.filter(regex='e$', axis=1)
one three
mouse 1 3
rabbit 4 6
# select rows containing 'bbi'
df.filter(like='bbi', axis=0)
one two three
rabbit 4 5 6
xxxxxxxxxx
1
2
3
4
# filter rows for year 2002 using the boolean expression
>gapminder_2002 = gapminder[gapminder['year']==2002]
>print(gapminder_2002.shape)
(142, 6)
xxxxxxxxxx
alldata_balance = alldata[(alldata[IBRD] !=0) | (alldata[IMF] !=0)]