样本DF:
placeholder: (context, url) => new CircularProgressIndicator(),
预期DF:
ID Name Price Amount Fit_Test
1 Apple 10 15 Super_Fit
2 Apple 10 0 Super_Fit
3 Apple 10 0 Super_Fit
4 Orange 12 20 Not_Fit
5 Orange 12 0 Not_Fit
6 Banana 15 17 Medium_Fit
7 Banana 15 0 Medium_Fit
8 Pineapple 25 19 Medium_Fit
9 Pineapple 25 18 Medium_Fit
10 Cherry 30 56 Super_Fit
11 XXX 50 0 Medium_Fit
12 XXX 50 0 Medium_Fit
问题陈述:
我想用 ID Name Price Amount Fit_Test
1 Apple 10 15 Super_Fit
2 Apple 10 0 Super_Fit
3 Apple 10 0 Super_Fit
4 Orange 12 20 Not_Fit
6 Banana 15 17 Medium_Fit
8 Pineapple 25 19 Medium_Fit
9 Pineapple 25 18 Medium_Fit
10 Cherry 30 56 Super_Fit
11 XXX 50 0 Medium_Fit
12 XXX 50 0 Medium_Fit
和group-by
Name
,然后基于Price
并以Amount
作为条件列进行过滤。
如果Fit_Test为Fit_Test
,则不需要no操作。 (行1,2,3和10在输入和期望DF中是相同的)
如果在Super_Fit
和Name
条件下且Fit_Test不是Price
,请检查其是否具有Super_Fit
作为Amount
的{{1}}然后删除该行(删除ID 4,5,6,7以及“预期5和7”中的ID)
如果在0
和Name
条件内,并且Fit_Test不是Price
,如果合同金额大于零,则不要删除任何行(第8行和第9行在Input和Expected DF中相同)
如果在Super_Fit
和Name
条件内,并且Fit_Test不是Price
,如果合同金额的所有值均等于零,则不删除任何行(第11行和第12行在Input和Expected DF中相同)
我可以做一个解决方案,它删除所有零但对条件列没有帮助
答案 0 :(得分:1)
您可以链接2个条件-比较Fit_Test
,并按GroupBy.transform
和GroupBy.all
检查每组是否所有True
,并进行第二次比较是否不相等:
m1 = df['Fit_Test'].eq('Super_Fit').groupby([df['Name'],df['Price']]).transform('all')
m2 = df['Amount'].ne(0)
df = df[m1 | m2]
print (df)
ID Name Price Amount Fit_Test
0 1 Apple 10 15 Super_Fit
1 2 Apple 10 0 Super_Fit
2 3 Apple 10 0 Super_Fit
3 4 Orange 12 20 Not_Fit
5 6 Banana 15 17 Medium_Fit
7 8 Pineapple 25 19 Medium_Fit
8 9 Pineapple 25 18 Medium_Fit
9 10 Cherry 30 56 Super_Fit