1

I have dataframe contains ACQ/REL pair recusively as below:

import pandas as pd

data = [
    ['2023-06-05 16:51:27.561','ACQ','location'],    
    ['2023-06-05 16:51:27.564','ACQ','location'],
    ['2023-06-05 16:51:27.567','ACQ','location'],
    ['2023-06-05 16:51:27.571','REL','location'],
    ['2023-06-05 16:51:27.573','REL','location'],
    ['2023-06-05 16:51:27.587','REL','location'],
    ['2023-06-05 16:51:28.559','ACQ','location'],
    ['2023-06-05 16:51:28.561','ACQ','location'],
    ['2023-06-05 16:51:28.563','ACQ','location'],
    ['2023-06-05 16:51:28.566','REL','location'],
    ['2023-06-05 16:51:28.569','REL','location'],
    ['2023-06-05 16:51:28.575','REL','location']
]

df = pd.DataFrame(data,columns=['ts','action','name'])

I would re-orgnize it by ACQ/REL pairs, the outer ACQ/REL pairs as a group, so that the output dataframe looks like below:

0   2023-06-05 16:51:27.561    ACQ  location
5   2023-06-05 16:51:27.587    REL  location
1   2023-06-05 16:51:27.564    ACQ  location
4   2023-06-05 16:51:27.573    REL  location
2   2023-06-05 16:51:27.567    ACQ  location
3   2023-06-05 16:51:27.571    REL  location
6   2023-06-05 16:51:28.559    ACQ  location
11  2023-06-05 16:51:28.575    REL  location
7   2023-06-05 16:51:28.561    ACQ  location
10  2023-06-05 16:51:28.569    REL  location
8   2023-06-05 16:51:28.563    ACQ  location
9   2023-06-05 16:51:28.566    REL  location

Current example is 3 pairs a group but it's not constantly the same. What's proper way to get such results?

3 Answers 3

1

Using a list as a stack, you can compute offsets for the REL that indicate how far back the corresponding ACQ is located (i.e. the ACQ's index). Then sort the indices according to the adjusted positions (ACQ remaining at their original position and REL offset back to the ACQ's position) to get the indexes in the new order:

acq     = list()                                # stack of ACQ positions
iREL    = enumerate(x=="REL" for _,x,_ in data) # identify REL indices

offsets = (acq.pop() if rel else acq.append(i) or i for i,rel in iREL)
order   = (i for i,_ in sorted(enumerate(offsets),key=lambda x:x[::-1]))

data    = [data[i] for i in order]   # reorder data : df.reindex(order)

print(*data,sep="\n")

['2023-06-05 16:51:27.561', 'ACQ', 'location']
['2023-06-05 16:51:27.587', 'REL', 'location']
['2023-06-05 16:51:27.564', 'ACQ', 'location']
['2023-06-05 16:51:27.573', 'REL', 'location']
['2023-06-05 16:51:27.567', 'ACQ', 'location']
['2023-06-05 16:51:27.571', 'REL', 'location']
['2023-06-05 16:51:28.559', 'ACQ', 'location']
['2023-06-05 16:51:28.575', 'REL', 'location']
['2023-06-05 16:51:28.561', 'ACQ', 'location']
['2023-06-05 16:51:28.569', 'REL', 'location']
['2023-06-05 16:51:28.563', 'ACQ', 'location']
['2023-06-05 16:51:28.566', 'REL', 'location']

If the data is already in the dataframe, the order list could be used to sort the dataframe with the reindex() method

1

Try this:

df.sort_values('ts').assign(sortkey=df.groupby('action').cumcount()).sort_values(['sortkey','action'])

Output:

                         ts action      name  sortkey
0   2023-06-05 16:51:27.561    ACQ  location        0
3   2023-06-05 16:51:27.571    REL  location        0
1   2023-06-05 16:51:27.564    ACQ  location        1
4   2023-06-05 16:51:27.573    REL  location        1
2   2023-06-05 16:51:27.567    ACQ  location        2
5   2023-06-05 16:51:27.587    REL  location        2
6   2023-06-05 16:51:28.559    ACQ  location        3
9   2023-06-05 16:51:28.566    REL  location        3
7   2023-06-05 16:51:28.561    ACQ  location        4
10  2023-06-05 16:51:28.569    REL  location        4
8   2023-06-05 16:51:28.563    ACQ  location        5
11  2023-06-05 16:51:28.575    REL  location        5
1

If it is always guaranteed that there are the same number of ACQ and REL, and that they are respectively in correct order (i.e., the n-th ACQ should always be paired with the n-th REL), then the solution is to split the original list into two first, by its second element, then loop to output one from each list.

Code:

import pandas as pd

data = [
    ['2023-06-05 16:51:27.561', 'ACQ', 'location'],
    ['2023-06-05 16:51:27.564', 'ACQ', 'location'],
    ['2023-06-05 16:51:27.567', 'ACQ', 'location'],
    ['2023-06-05 16:51:27.571', 'REL', 'location'],
    ['2023-06-05 16:51:27.573', 'REL', 'location'],
    ['2023-06-05 16:51:27.587', 'REL', 'location'],
    ['2023-06-05 16:51:28.559', 'ACQ', 'location'],
    ['2023-06-05 16:51:28.561', 'ACQ', 'location'],
    ['2023-06-05 16:51:28.563', 'ACQ', 'location'],
    ['2023-06-05 16:51:28.566', 'REL', 'location'],
    ['2023-06-05 16:51:28.569', 'REL', 'location'],
    ['2023-06-05 16:51:28.575', 'REL', 'location']
]

df = pd.DataFrame(data, columns=['ts', 'action', 'name'])
# print(df)

data_acq = []
data_rel = []

for index, row in df.iterrows():
    if row['action'] == 'ACQ':
        data_acq.append(row)
    elif row['action'] == 'REL':
        data_rel.append(row)
assert len(data_acq) == len(data_rel)

df_new = pd.DataFrame([], columns=['ts', 'action', 'name'])

for j in range(len(data_acq)):
    df_new = pd.concat([
        df_new,
        pd.DataFrame([
            data_acq[j], data_rel[j]
        ])
    ])
print(df_new)

Output:

                         ts action      name
0   2023-06-05 16:51:27.561    ACQ  location
3   2023-06-05 16:51:27.571    REL  location
1   2023-06-05 16:51:27.564    ACQ  location
4   2023-06-05 16:51:27.573    REL  location
2   2023-06-05 16:51:27.567    ACQ  location
5   2023-06-05 16:51:27.587    REL  location
6   2023-06-05 16:51:28.559    ACQ  location
9   2023-06-05 16:51:28.566    REL  location
7   2023-06-05 16:51:28.561    ACQ  location
10  2023-06-05 16:51:28.569    REL  location
8   2023-06-05 16:51:28.563    ACQ  location
11  2023-06-05 16:51:28.575    REL  location

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.