2017-04-13 70 views
-1

我来自Java背景,并通过将Python应用于工作环境中来学习Python。我有一个功能强大的代码,我真的想要改进。将最大值分组返回N个属性

本质上,我有一个列表namedvalue有3个数值和1个时间值。

complete=[] 
uniquecomplete=set() 
screenedPartitions = namedtuple('screenedPartitions'['feedID','partition','date', 'screeeningMode']) 

我解析日志,这被填充后,我想创建一个减少的组基本上是最近日期成员,其中feedID,分区和screeningMode是相同的。到目前为止,我只能通过使用一个讨厌的嵌套循环来解决它。

for a in complete: 
    max = a    
    for b in complete: 
     if a.feedID == b.feedID and a.partition == b.partition and\ 
         a.screeeningMode == b.screeeningMode and a.date < b.date: 
      max = b 
    uniqueComplete.add(max) 

任何人都可以给我如何改善这个建议吗?将stdlib中的可用内容解决出来是件好事,因为我想我的主要任务是让我用map/filter功能来考虑它。

的数据看起来类似于

FeedID | Partition | Date   | ScreeningMode 

68  | 5  |10/04/2017 12:40| EPEP 

164 | 1  |09/04/2017 19:53| ISCION 

164 | 1  |09/04/2017 20:50| ISCION 

180 | 1  |10/04/2017 06:11| ISAN 

128 | 1  |09/04/2017 21:16| ESAN 

所以 运行代码行2之后的第3行是一个较新的版本将被删除。

铊;博士,将这个SQL使用Python什么:

SELECT feedID,partition,screeeningMode,max(date) 
from Complete 
group by 'feedID','partition','screeeningMode' 

回答

0

尝试是这样的:

import pandas as pd 

df = pd.DataFrame(screenedPartitions, columns=screenedPartitions._fields) 
df = df.groupby(['feedID','partition','screeeningMode']).max() 

这真的取决于你的日期是如何表示的,但如果你提供数据我认为我们可以解决一些问题。

+0

回复的职业生涯。日期是一个日期时间对象,datetime.datetime.strptime(stringDate,“%d /%m /%Y%H:%M:%S”)和上面添加的示例。我见过熊猫,但试图在标准库中解决它。 – whatafarce