1
\$\begingroup\$

I have this snippet, which computes on two columns, and if I get some increasing pattern then I will consider it a valid Key. How can I optimize this in pandas?

Sample Code

Here there is only one group of track_id. However, I will have many track_id groups, I need to apply the same for all groups.

import pandas as pd
import numpy as np

tid = [5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5.,
       5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5.]

j = [ 0.,  0.,  0.,  0.,  0.,  1., 52., 53., -1., -1., -1., -1., -1.,
       -1., -1., -1., -1.,  1., -1., -1., -1., -1., -1., -1., -1., -1.,
       -1., -1.]

k = [ 0.,  0.,  0.,  0.,  0., -1., -1., -1., -1., -1., 56., 57., 58.,
       59., 60., 61., 62., 63., -1., -1., -1., -1., -1., -1., -1., -1.,
       -1., -1.]
rules = ['rule_1_start', 'rule_1_end']
data = {'rule_1_start': j, 'rule_1_end': k, 'track_id': tid }
df = pd.DataFrame.from_dict(data)
for r in range(0, len(rules), 2):
    track_groups = df.groupby('track_id')
    for key, item in track_groups:
        _min = np.argmin(item[rules[r]].values)
        _max =  np.argmax(item[rules[r+1]].values)
        if _min < _max: 
            print(f"{key}")

The pattern

I am trying to find is something like this. If you see these two columns, there is increasing value pattern. Col 1 has 77, 78,79 and col 2 has 82-91. The pattern should be increasing from col1 to col2 but not vice versa.

enter image description here

\$\endgroup\$
3
  • 2
    \$\begingroup\$ The title of your question is too generic to be useful. The standard on this site is to simply state the task accomplished by the code, rather then your concerns about it. Also the question "how to run as lambda in python" seems to imply that something is not implemented and that may attract close votes. \$\endgroup\$ – slepic Sep 4 '20 at 5:36
  • 2
    \$\begingroup\$ Please show us more of your code. What does df actually look like? Not just an image of some of the columns, but ideally as code (or how to generate a toy example). \$\endgroup\$ – Graipher Sep 4 '20 at 6:04
  • \$\begingroup\$ updated with sample valid set, for simplicity I am just keeping one group. \$\endgroup\$ – ajayramesh Sep 4 '20 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.