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?
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}")
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.
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP