1
條件濾波器考慮類似下面的數據幀。Python的熊貓GROUPBY:根據上值
import pandas as pd
# Initialize dataframe
df1 = pd.DataFrame(columns=['bar', 'foo'])
df1['bar'] = ['001', '001', '001', '001', '002', '002', '003', '003', '003']
df1['foo'] = [-1, 0, 2, 3, -8, 1, 0, 1, 2]
>>> print df1
bar foo
0 001 -1
1 001 0
2 001 2
3 001 3
4 002 -8
5 002 1
6 003 0
7 003 1
8 003 2
# Lower and upper bound for desired range
lower_bound = -5
upper_bound = 5
我想用GROUPBY在大熊貓返回與符合條件的bar
濾除行的數據幀。特別是,我想篩選出與bar
行;如果此bar
的foo
的一個值是不是lower_bound
和upper_bound
之間。
在上述示例中,與bar = 002
行應該因爲並不是所有與bar = 002
行的被過濾掉包含的foo
-5
和5
之間的值(即,行索引4
包含foo = -8
)。這個例子的期望輸出如下。
# Desired output
bar foo
0 001 -1
1 001 0
2 001 2
3 001 3
6 003 0
7 003 1
8 003 2
我試過以下方法。
# Attempted solution
grouped = df1.groupby('bar')['foo']
grouped.filter(lambda x: x < lower_bound or x > upper_bound)
然而,這會產生一個TypeError: the filter must return a boolean result
。此外,當我想要結果返回一個數據框對象時,這種方法可能會返回一個groupby對象。