样品测向:
ID Name Match1 Random_Col Match2 Price Match3 Match4 Match5
1 Apple Yes Random Value No 10 Yes Yes Yes
2 Apple Yes Random Value1 No 10 Yes Yes No
3 Apple Yes Random Value2 No 15 No Yes Yes
4 Orange No Random Value Yes 12 Yes Yes No
5 Orange No Random Value Yes 12 No No No
6 Banana Yes Random Value No 15 Yes No No
7 Apple Yes Random Value No 15 No Yes Yes
预期DF:
ID Name Match1 Random_Col Match2 Price Match3 Match4 Match5 Final_Match
1 Apple Yes Random Value No 10 Yes Yes Yes Full
2 Apple Yes Random Value1 No 10 Yes Yes No Partial
3 Apple Yes Random Value2 No 15 No Yes Yes Partial
4 Orange No Random Value Yes 12 Yes Yes No Full
5 Orange No Random Value Yes 12 No No No Partial
6 Banana Yes Random Value No 15 Yes No No Full
7 Apple Yes Random Value No 15 No Yes Yes Partial
问题陈述:
Name
和Price
是非重复的,只需将Full
放在Final_Match
列中(示例ID 6)如果组合Name
和Price
是重复的,那么在这些组合中,在Match1到Match5列中计算Yes
,无论哪个列的“Yes”越大,则为其中一列放入Full
,为另一列放入Partial
(示例ID 1&;2和4,5)
如果组合Name
和Price
是重复的,那么在Match1到Match5列中的ID计数Yes
内,如果它们具有相等的“Yes”,则将Partial
放在这两个列中(示例ID 3,7)
代码
s = (df.replace({'Yes': 1, 'No': 0})
.iloc[:, 1:]
.sum(1))
df['final_match'] = np.where(s.groupby(df[['Price','Name']]).rank(ascending=False).eq(1), 'Full ','Partial')
当我只需要groupby
一列时,上面的代码就可以工作了,比如说Name
,但是它不能用于组合。你知道吗
任何帮助!!你知道吗
用途:
相关问题 更多 >
编程相关推荐