简体   繁体   中英

Pandas Dataframe get minimum difference between two columns

Dataframe

d = {'Resource': ['A','A','A','B','B','B'], 'User': ['1','2','3','4','5','6'], 'earliestSlot': [1,2,3,5,4,6], 'latestSlot': [1.2,2.5,3.9,6,5,6.1]}
pd.DataFrame(data=d)

I want to aggregate the data in a way that calculates the minimum difference between latestSlot and earliestSlot of different users of the same resource. Basically I want to calculate the minimum idle time of each resource before the next user accesses it.

Target Data

Resource MinHeadway
A 0.5
B 0

I have the following code but I am sure there is a faster method.

´´´

def get_min_headway(resource_id):
    latestSlots = d[d['Resource'] == resource_id].latestSlot
    earliestSlots = d[d['Resource'] == resource_id].earliestSlot
    min_headway = float('inf')
    for time in latestSlots:
        headways = earliestSlots - time
        for headway in headways:
            if headway >= 0:
                if headway < min_headway:
                    min_headway = headway
    return min_headway

d['min_headway'] = d['Resource'].apply(get_min_headway)

´´´

It's not exactly clear what your desired outcome is, but by minimum idle time , I assumed you wanted the minimum difference between "latestSlot" of the previous user and the "earliestSlot" of the next user (since an unused time is an idle time ). So in that case, you can use the following.

We sort by "earliestSlot"; then groupby "Resource" and do exactly what's explained above.

out = (df.sort_values(by='earliestSlot')
       .groupby('Resource')
       .apply(lambda x: (x['earliestSlot']-x['latestSlot'].shift()).min())
       .reset_index()
       .rename(columns={0:'MinHeadway'}))

Output:

  Resource  MinHeadway
0        A         0.5
1        B         0.0

The same result could also be obtained without applying a lambda:

tmp = df.sort_values(by='earliestSlot')
out = (tmp.groupby('Resource')['latestSlot'].shift()
       .rsub(tmp['earliestSlot'])
       .groupby(x['Resource']).min()
       .reset_index()
       .rename(columns={0:'MinHeadway'}))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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