Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Currently I am using the following function;

 df['i'] = df.groupby(['i']).filter(lambda i: len(i) > 500)

This works as intended, tested on other data frames, except when dealing with large quantities of groups. I am trying to use this with around 50,000 groups and have thus far not seen my program process this line. The longest I have let the program run is a bit under 48 hours.

Edit: The method works fine for large groups assuming the lambda function does not remove all the groups. decreasing the minimum length a group can be to 250 allowed the program to execute within 30 seconds.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
3.8k views
Welcome To Ask or Share your Answers For Others

1 Answer

This is a case for parallel computing if your processor has multiple cores...

from multiprocessing import Pool, cpu_count

def applyParallel(dfGrouped, func):
    with Pool(cpu_count()) as p:
        ret_list = p.map(func, [group for name, group in dfGrouped])
    return pandas.concat(ret_list)

this does not cover every situation that you put a function on but will cover yours.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...