claim_data
Вот как выглядит моя функция.
Код: Выделить всё
def flag_date_within_m_months(group, col_label, col_date):
group[col_date] = pd.to_datetime(group[col_date])
group = group.sort_values(by=col_date) # Sort by date
group[col_label] = False
# Create a rolling window to count claims
for i in range(len(group)):
current_date = group[col_date].iloc[i]
window_start = current_date - pd.DateOffset(months=m)
# Count the number of claims within the window
count_within_window = group[(group[col_date] = window_start)].shape[0]
if count_within_window > n:
# Flag all claims in the window
group.loc[(group[col_date] = window_start), col_label] = True
print(f"Flagging claims between {window_start} and {current_date}") # Debug output
return group
Код: Выделить всё
# Control parameters
n = 5
m = 6
# Create an index so that every row with the same 'Client_number's_PO' and 'Benefit/component_code' has the same index number.
claim_data["1. Index"] = claim_data_m.groupby(["Client_number's_PO", "Benefit/component_code"]).ngroup()
# Flag the rows based on the condition
claim_data["1"] = False
claim_data.groupby("1. Index").apply(flag_date_within_m_months, col_label="1", col_date="Incur Date")
Я попробовал то, что описал выше, и ожидаю, что за m месяцев появятся строки с более чем n вхождениями с те же «Client_number's_PO» и «Benefit/comComponent_code» будут помечены как True в столбце «1».
Подробнее здесь: https://stackoverflow.com/questions/791 ... within-the
Мобильная версия