检查Pandas Dataframe组中的一列中是否有2个特定值,然后返回这些行

时间:2018-10-25 10:15:29

标签: python-3.x pandas pandas-groupby

我有一个groupby对象。对于这些组中的每一个,我需要检查特定列是否包含包含值A和值B的行,并仅返回该组中的那两行。如果我使用isin或“ |”我会得到其中两个值之一存在的情况。现在,我正在草率工作,先检查第一个条件,然后检查第二个条件(如果第一个条件为真),然后将两个检查的结果连接起来。

我的代码如下:

import pandas as pd
from datetime import datetime, timedelta
from statistics import mean

dict = {'col-a': ['T1A', 'T1A', 'T1A', 'T1B', 'T1B', 'T1C', 'T1C', 'P1', 'P1'],
        'col-b': ['07:57:00', '09:00:00', '12:00:00', '08:00:00', '08:25:00', '08:15:00', '07:25:00', '10:00:00',  '07:45:00'],
        'col-c': ['11111', '22222', '99999', '33333', '22222', '22222', '99999', '22222', '99999'],
        'col-d': ['07:58:00', '09:01:00', '12:01:00', '08:01:00', '08:26:00', '08:16:00', '07:26:00', '10:01:00',  '07:46:00'],
        }

original_df = pd.DataFrame(dict)
print("original df\n", original_df)

# condition 1: must contain T1 in col-a
# condition 2: must contain 22222(variable) amongst each group of col-a
# condition 3: record containing 22222 should have col-b value between 7 and 9
# condition 4: must contain 99999(stays the same) among amongst each group of col-a where above conditions are met


no_to_check = '22222' # comes from another dataframe column

# filtering rows where col-a contains T1
filtered_df = original_df[original_df['col-a'].str.contains('T1')]

# grouping by col-a
trip_groups = filtered_df.groupby('col-a')

# checking if it contains '22222' in column c and '22222' has time between 7 and 9 in column b
trips_time_dict = {}
for group_key, group in trip_groups:
    check1 = group[(group['col-c'] == no_to_check) & (group['col-b'].between('07:00:00', '09:00:00'))]

    if len(check1) != 0:
        # checking if the group contains '99999' in column c
        check2 = group[group['col-c'] == '99999']

        if len(check2) != 0:
            all_conditions = pd.concat([check1,check2])

对于每个符合条件的组,期望的输出应包含22222行和99999行。

1 个答案:

答案 0 :(得分:0)

IIUC,您可以将df作为原始数据帧执行以下操作:

df[df['col-a'].str.contains('T1')].groupby('col-a').apply(lambda x: x[(x['col-c']=='22222') & (x['col-b'].between('07:00:00', '09:00:00')) & (x['col-c']=='99999').any()])

收益:

        col-a     col-b  col-c     col-d
col-a                                   
T1A   1   T1A  09:00:00  22222  09:01:00
T1C   5   T1C  08:15:00  22222  08:16:00