Я новичок в Пандах, в настоящее время у меня есть серия, подобная этой:
import pandas as pd
index = [x for x in range(75860, 76510, 10)]
# number of occurrence
value = [1, 1, 4, 6, 7, 7, 7, 7, 8, 7, 7, 7, 8, 6, 6, 7, 15, 23, 26, 30, 31, 28, 22, 22, 21, 19, 14, 15, 15, 14, 12, 12, 13, 14, 14, 15, 15, 19, 19, 23, 25, 34, 38, 39, 40, 41, 35, 35, 30, 26, 23, 23, 29, 25, 25, 25, 23, 21, 19, 16, 14, 7, 6, 4, 1]
sample_ser = pd.Series(value, index=index)
Эта серия представляет меру и сколько раз они были подсчитаны.
Я пытаюсь вычислить пользовательские параметры, но я использую стандартный питон для циклов, я хочу знать, есть ли лучший способ сделать это, вот одна из функций.
Спасибо за помощь.
# return limits where 68% of total count took place
# starting from most_counted length we add the highest count closest to most_counted length
# if 2 count are equal we look for the next label, the one with highest count is choose
def active_area(sample_ser):
# this is the label we have the most occurrence
most_counted = 76310
target = sample_ser.sum()*0.68
total_count = 0
high_label = most_counted + 10
low_label = most_counted - 10
while total_count < target:
# index out of bound
if low_label < sample_ser.index[0]:
total_count += sample_ser[high_label]
high_label += 10
continue
# index out of bound
if high_label >= sample_ser.index[-1]:
total_count += sample_ser[low_label]
low_label -= 10
continue
h_len = sample_ser[high_label]
l_len = sample_ser[low_label]
if h_len > l_len:
total_count += h_len
high_label += 10
continue
if h_len < l_len:
total_count += l_len
low_label -= 10
continue
if h_len == l_len:
counter = 10
while True:
temp_high = high_label+counter
temp_low = low_label-counter
if temp_low < sample_ser.index[0]:
total_count += h_len
high_label += 10
break
if temp_high >= sample_ser.index[-1]:
total_count += l_len
low_label -= 10
break
h_len_temp = sample_ser[temp_high]
l_len_temp = sample_ser[temp_low]
if h_len_temp > l_len_temp:
total_count += h_len
high_label += 10
break
if h_len_temp < l_len_temp:
total_count += l_len
low_label -= 10
break
if h_len_temp == l_len_temp:
counter += 10
continue
if low_label < sample_ser.index[0]:
low_label = sample_ser.index[0]
if high_label >= sample_ser.index[-1]:
high_label = sample_ser.index[-1]
return high_label, low_label
edit: удалено 3 из 4 for loop из начального вопроса, вам будет проще ответить