简体   繁体   English

Python / Pandas /纸浆优化重复项

[英]Python / Pandas / Pulp Optimization Duplicates

I am trying to optimize a grouping / selection of trial members with limited space, and am running into some trouble. 我正在尝试在有限的空间内优化对试验成员的分组/选择,并且遇到了一些麻烦。 I have the pandas data frames ready for optimization, and can run the linear optimization with no problems, except for one constraint I need to add. 我已经准备好进行优化的pandas数据框,并且可以运行线性优化而没有问题,除了需要添加的一个约束之外。 I am trying to use binaries for selection (but I am not tied to that for any reason, so if a different method would resolve this, I could switch) from a large list. 我正在尝试使用二进制文件进行选择(但是由于任何原因我都没有选择二进制文件,因此如果使用其他方法可以解决此问题,则可以切换)。 I need to minimize combined trial time for selection in the next round of trials, but some subjects already ran multiple trials due to the nature of the experiment. 在下一轮试验中,我需要最小化组合试验时间以进行选择,但是由于试验的性质,一些受试者已经进行了多次试验。 I would like to select the best combination of subjects based on minimizing time, but allow some subjects to be in the list multiple times for the optimization (so I do not have to manually remove them beforehand). 我想基于最短的时间来选择主题的最佳组合,但允许某些主题多次出现在列表中以进行优化(因此我不必事先手动删除它们)。 For instance: 例如:

Name         Trial    ID       Time (ms)    Selected?
Mary Smith   A        11001    33           1
John Doe     A        11002    24           0
James Smith  B        11003    52           0
Stacey Doe   A        11004    21           1
John Doe     B        11002    19           1

Is there some way to allow 2 John Doe entries for the optimization but constrain the output to only one selection of him? 有什么方法可以允许2个John Doe条目进行优化,但是将输出限制为只选择其中一个? Thanks for your time! 谢谢你的时间!

If you have a requirement to record all the values you want to remove, you could use the duplicated function, like this 如果您需要记录要删除的所有值,则可以使用duplicated函数,如下所示

# First sort your dataframe
df.sort_values(['Name', 'Time (ms)'], inplace=True)

# Make a new column of duplicated values based only on name
df['duplicated'] = df.duplicated(subset=['Name'])

# You can then access the duplicates, but still have a log of the rejects
df.query('not duplicated')
#           Name Trial     ID  Time (ms)  Selected?  duplicated
# 2  James Smith     B  11003         52          0       False
# 1     John Doe     A  11002         24          0       False
# 0   Mary Smith     A  11001         33          1       False
# 3   Stacey Doe     A  11004         21          1       False

df.query('duplicated')
#        Name Trial     ID  Time (ms)  Selected?  duplicated
# 4  John Doe     B  11002         19          1        True

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

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