I learned a lot from the best answer answered by @JAponte. But it is a little wrong. If values becomes
values = [0.940353, 0.999144, 0.909454, 0.904968, 0.867957, 0.801426, 0.794733, 0.770106, 0.741985, 0.671444, 0.558297, 0.496972, 0.457803, 0.446388, 0.430217, 0.379902, 0.321828, 0.298304, 0.442079, 0.634764,]
the result will be output :
count percentage
0 17 31.7
The percentage is obtained by 0.29830 / 0.94035, but the correct answer should be 0.29830 / 0.999144 = 0.299. So I made the following changes:
import pandas as pd
def get_decreasing_count_and_percentage(df, min_count=7):
is_dec = df["value"].diff().lt(0).values
cnt = 0
starting_value = None
first_time = True
result = []
for i in range(len(is_dec)):
if is_dec[i]:
cnt += 1
if first_time:
starting_value = df["value"].values[i - 1]
first_time = False
else:
if cnt > 0:
percent = round((df["value"].values[i - 1] / starting_value) * 100., 1)
result.append((cnt + 1, percent))
cnt = 0
first_time = True
result_df = pd.DataFrame.from_records(result, columns=['count', 'percentage'])
return result_df[result_df["count"] >= min_count]
times = ['11:55', '12:00', '12:05', '12:10', '12:15', '12:20', '12:25', '12:30', '12:35', '12:40', '12:45', '12:50',
'12:55', '13:00', '13:05', '13:10', '13:15', '13:20', '13:25', '13:30']
values = [0.940353, 0.999144, 0.909454, 0.904968, 0.867957, 0.801426, 0.794733, 0.770106, 0.741985, 0.671444, 0.558297,
0.496972, 0.457803, 0.446388, 0.430217, 0.379902, 0.321828, 0.298304, 0.442079, 0.634764, ]
my_df = pd.DataFrame(data={'time': times, 'value': values})
print(get_decreasing_count_and_percentage(my_df))
values2 = [0.940353, 0.919144, 0.909454, 0.904968, 0.867957, 0.801426, 0.894733, 0.770106, 0.741985, 0.671444, 0.558297,
0.496972, 0.457803, 0.446388, 0.430217, 0.379902, 0.321828, 0.298304, 0.442079, 0.634764]
my_df2 = pd.DataFrame(data={'time': times, 'value': values2})
print(get_decreasing_count_and_percentage(my_df2))
print(get_decreasing_count_and_percentage(my_df2, min_count=6))