0

I have one column, and another is datetime which is in index position I done some coding on this. I try to use for loop for below condition but need optimization or list comprehension.

date_today = datetime.now()
days = pd.date_range(date_today, date_today + timedelta(254), freq='D')
dd = pd.DataFrame()
cc = sorted(np.random.randint(0,32,255))
dd['mean_base'] = cc
dd['time'] = days
dd.set_index('time',inplace=True)
rle = [(k, sum(1 for i in g)) for k,g in itertools.groupby(dd['mean_base'])]
# output Ex: rle = [(66,3),(35,12),(66,185),(35,190)]
count1 = 0 
for i in rle:
    if i[0] >= 65:
        if i[1] >= 185:
            count1 += 1
        else:
            pass
# Suggested by someone
count2 = sum([np.floor(i[1]/rolling_window) for i in rle if i[0]>=1])

# I didn't get this. (May be need changes)
print(count1)
#output: 1

screenshot

enter image description here

Is there any other way or how can I use list comprehension or code optimization?

8
  • What is your expected output? Commented Oct 20, 2022 at 10:34
  • measurement of count like if that condition satisfies then count shows 12 or 13 or etc.. Commented Oct 20, 2022 at 10:37
  • It is hard to understand this transformation unless you can show us sample input and expected output dataframes. Commented Oct 20, 2022 at 11:42
  • @AzharKhan Input file is there.. Output doesn't have the dataframe.. It is just one int which stored in count .. Only I required the count. The question can I use list comprehension for this. Commented Oct 20, 2022 at 12:14
  • You want to calculate count1 or count2? Commented Oct 20, 2022 at 12:22

1 Answer 1

0

IIUC, you can try

count1 = sum([(i >= 2) and (j >= 32) for i, j in rle])
# or with pandas
count1 = len(pd.DataFrame(rle).loc[lambda df: df[0].ge(2) & df[1].ge(32), :])
Sign up to request clarification or add additional context in comments.

3 Comments

just I edited question. This may helpful..
@SushilKokil I provide two solutions here, does it help? Your nested if statement can be directly replaced by (i[0] >= 65) and (i[1] >= 185).
Partially it works. Thank you.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.