简体   繁体   English

使用包括 OR 在内的多个条件过滤 dataframe

[英]Filter dataframe with multiple conditions including OR

I wrote a little script that loops through constraints to filter a dataframe. Example and follow up explaining the issue are below.我写了一个小脚本,循环通过约束来过滤 dataframe。示例和后续解释问题如下。

constraints = [['stand','==','L'],['zone','<','20']]
    
for x in constraints:
    vari = x[2]
    df = df.query("{0} {1} @vari".format(x[0],x[1]))
zone stand站立 speed速度 type类型
0 0 2 2个 L大号 83.7 83.7 CH CH
1 1个 7 7 L大号 95.9 95.9 SI国际单位制
2 2个 14 14 L大号 94.9 94.9 FS FS
3 3个 11 11 L大号 93.3 93.3 FS FS
4 4个 13 13 L大号 86.9 86.9 CH CH
5 5个 7 7 L大号 96.4 96.4 SI国际单位制
6 6个 13 13 L大号 82.6 82.6 SL SL

I can't figure out a way to filter when there is an OR condition.当有 OR 条件时,我想不出一种过滤方法。 For example, in the table above I'd like to return a dataframe using the constraints in the code example along with any rows that contain SI or CH in the type column.例如,在上表中,我想使用代码示例中的约束以及类型列中包含 SI 或 CH 的任何行返回 dataframe。 Does anyone have ideas on how to accomplish this?有没有人对如何实现这一点有想法? Any help would be greatly appreciated.任何帮助将不胜感激。

This seems to have gotten the job done but there is probably a much better way of going about it.这似乎已经完成了工作,但可能还有更好的方法。

for x in constraints:
    vari = x[2]
    if isinstance(vari,list):
        frame = frame[frame[x[0]].isin(vari)]
    else:          
        frame = frame.query("{0} {1} @vari".format(x[0],x[1]))

IIUC (see my question in the comment) you can do it like this: IIUC(在评论中查看我的问题)你可以这样做:

Made a little different df to show you the result (I guess the table you show is already filtered)做了一些不同的 df 来向你展示结果(我猜你展示的表格已经被过滤了)

df = pd.DataFrame(
    {'zone': {0: 2, 1: 11, 2: 25, 3: 11, 4: 23, 5: 7, 6: 13},
     'stand': {0: 'L', 1: 'L', 2: 'L', 3: 'C', 4: 'L', 5: 'K', 6: 'L'},
     'speed': {0: 83.7, 1: 95.9, 2: 94.9, 3: 93.3, 4: 86.9, 5: 96.4, 6: 82.6},
     'type': {0: 'CH', 1: 'SI', 2: 'FS', 3: 'FS', 4: 'CH', 5: 'SI', 6: 'SL'}})
print(df)


    zone    stand   speed   type
0      2        L    83.7     CH
1     11        L    95.9     SI
2     25        L    94.9     FS
3     11        C    93.3     FS
4     23        L    86.9     CH
5      7        K    96.4     SI
6     13        L    82.6     SL


res = df.loc[ ( (df['type']=='SI') | (df['type']=='CH') ) & ( (df['zone']<20) & (df['stand']=='L') ) ]
print(res)


    zone    stand   speed   type
0      2        L    83.7     CH
1     11        L    95.9     SI

Let me know if that is what you are searching for.让我知道这是否是您要寻找的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM