AnswerBun.com

Computing on two colums

Code Review Asked by ajayramesh on October 24, 2020

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

Add your own answers!

Related Questions

C++ linked list inheriting from node class

1  Asked on November 17, 2021 by devinthenrough

       

MMMRjs a product of BYTES Genesis

3  Asked on November 17, 2021 by syed-mohammad-sannan

         

Simple marker rendering

1  Asked on November 17, 2021

     

C++20 ScopeGuard

1  Asked on November 15, 2021

   

LeetCode 37: Sudoku Solver

1  Asked on November 13, 2021

         

Converting a float to a fraction as a string

1  Asked on November 11, 2021 by stonkilla4

   

C# consecutive and redundant null checks

6  Asked on November 11, 2021 by frederik-hoeft

   

Format string processing similar to date or printf()

2  Asked on November 11, 2021 by domsson

     

Improvement to Luhn-Checksum Algorithm in Java

2  Asked on November 11, 2021 by elauser

   

Full Adder in Verilog

1  Asked on November 8, 2021

 

Python palindrome checker function

4  Asked on November 8, 2021 by dicomp

     

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir