删除首次出现特定值以下的行

时间:2019-01-01 16:31:57

标签: python pandas dataframe group-by pandas-groupby

目标是确定每个唯一c的{​​{1}}中的dcodeID之后发生,然后对数据帧进行子集化( groupby也一样)不包含那些a

样本df:

ID

对于此数据集,应从df中删除df = pd.DataFrame({'ID':np.repeat([1,2,3],[3,4,3]), 'dates' : [(pd.Timestamp('2018-12-30 00:00:00') + pd.Timedelta(hr, unit = 'h')) for hr in range(10)], 'cd': ['c','b','a','b','c','a','c','b','a','d']}) grouped = df.groupby('ID', as_index = False).apply(lambda x: x.sort_values('dates', ascending = False)) grouped = grouped.groupby('ID') ID==2

ID==3

输出:

for key, item in grouped:
    print(grouped.get_group(key), "\n\n")

我认为以下实现了此目标:在每个组中创建一个字符串,然后使用 ID dates cd 0 2 1 2018-12-30 02:00:00 a 1 1 2018-12-30 01:00:00 b 0 1 2018-12-30 00:00:00 c ID dates cd 1 6 2 2018-12-30 06:00:00 c 5 2 2018-12-30 05:00:00 a 4 2 2018-12-30 04:00:00 c 3 2 2018-12-30 03:00:00 b ID dates cd 2 9 3 2018-12-30 09:00:00 d 8 3 2018-12-30 08:00:00 a 7 3 2018-12-30 07:00:00 b 确定优先级:

str.find()

输出:

for i,r in grouped:
    cdSeq = ''
    for code in r['cd']:
        cdSeq+=code
    if abs(cdSeq.find('c')) < abs(cdSeq.find('a')) or abs(cdSeq.find('d'))<abs(cdSeq.find('a')):
        df = df[df['ID']!=i]

df.groupby('ID', as_index = False).apply(lambda x: x.sort_values('dates', ascending = False))

是否有更简洁,有效和/或Pythonic的方式来实现这一目标?

1 个答案:

答案 0 :(得分:0)

首先,确定每个组的“ a”之后出现的行为export class RegisterComponent implements OnInit { form: FormGroup; password = new FormControl("", [RxwebValidators.required()]); ConfirmPassword = new FormControl("", [RxwebValidators.compare({fieldName:'password '}); constructor(private fb: FormBuilder) { } ngOnInit() { this.form = this.fb.group({ "password": this.password, "ConfirmPassword ": this.ConfirmPassword }); } 。然后,确定哪些行包含“ c”和“ d”,如u。使用v,我们确定哪些行符合我们的条件,然后使用mulgroupby得到一个掩码以消除它们。

any