基于列中的两个可能值筛选Pandas DF

2024-10-02 16:22:55 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我有一个df,看起来像这样:

Created UserID  Service
1/1/2016    a   CWS
1/2/2016    a   Other
3/5/2016    a   Drive
2/7/2017    b   Enhancement
... ... ...

我想根据CWS和Drive的“Service”列中的值过滤它。我是这样做的:

^{pr2}$

它不起作用。有什么想法吗?在


Tags: dfservicedriveotheruseridcreatedcwsenhancement
2条回答

需要按位比较|or):

df=df[(df.Service=="CWS") | (df.Service=="Drive")]

最好是使用^{}

^{pr2}$

或使用^{}

df = df.query('Service=="CWS" | Service=="Drive"')

^{} with ^{}

df = df.query('Service== ["Other", "Drive"]')

print (df)
    Created UserID Service
1  1/2/2016      a   Other
2  3/5/2016      a   Drive

您也可以使用pandas.Series.str.match

df[df.Service.str.match('CWS|Drive')]

    Created UserID Service
0  1/1/2016      a     CWS
2  3/5/2016      a   Drive

其他口味
为了好玩!!

numpy-fi

^{pr2}$

添加numexpr

import numexpr as ne

s = df.Service.values
c1 = s == 'CWS'
c2 = s == 'Drive'
df[ne.evaluate('c1 | c2')]

时间
isin是赢家!str.match是输家:-(

np.random.seed([3,1415])
df = pd.DataFrame(dict(
        Service=np.random.choice(['CWS', 'Drive', 'Other', 'Enhancement'], 100000)))

%timeit df[(df.Service == "CWS") | (df.Service == "Drive")]
%timeit df[df.Service.isin(["CWS", "Drive"])]
%timeit df.query('Service == "CWS" | Service == "Drive"')
%timeit df.query('Service == ["Other", "Drive"]')
%timeit df.query('Service in ["Other", "Drive"]') 
%timeit df[df.Service.str.match('CWS|Drive')]

100 loops, best of 3: 16.7 ms per loop
100 loops, best of 3: 4.46 ms per loop
100 loops, best of 3: 7.74 ms per loop
100 loops, best of 3: 5.77 ms per loop
100 loops, best of 3: 5.69 ms per loop
10 loops, best of 3: 67.3 ms per loop

%%timeit
s = df.Service.values
c1 = s == 'CWS'
c2 = s == 'Drive'
df[c1 | c2]

100 loops, best of 3: 5.47 ms per loop

%%timeit 
import numexpr as ne

s = df.Service.values
c1 = s == 'CWS'
c2 = s == 'Drive'
df[ne.evaluate('c1 | c2')]

100 loops, best of 3: 5.65 ms per loop

相关问题 更多 >