I have the following DataFrame:
name date
test 2022-03-04
test 2022-03-05
test 2022-03-06
test 2022-03-17
test 2022-03-18
test 2022-03-21
test2 2022-03-04
test2 2022-03-05
test2 2022-03-15
test2 2022-03-19
test2 2022-03-21
test2 2022-04-16
test3 2022-03-14
test3 2022-03-15
test3 2022-03-23
test3 2022-03-27
test4 2022-03-20
test4 2022-04-15
test4 2022-04-17
test5 2022-03-01
test5 2022-03-04
test5 2022-03-06
test5 2022-03-12
test5 2022-04-04
test5 2022-04-10
test5 2022-04-14
test5 2022-05-04
test6 2022-03-05
test6 2022-03-15
test6 2022-06-20
test6 2022-06-24
test6 2022-06-27
How can I add a column old_data with value yes for duplicate old records in combination of (name , date) which have data at least greater than 3 values? The date column is in ascending order.
I want to produce this output:
name date old_data
test 2022-03-04 yes
test 2022-03-05 yes
test 2022-03-06 yes
test 2022-03-17
test 2022-03-18
test 2022-03-21
test2 2022-03-04 yes
test2 2022-03-05 yes
test2 2022-03-15 yes
test2 2022-03-19
test2 2022-03-21
test2 2022-04-16
test3 2022-03-14 yes
test3 2022-03-15
test3 2022-03-23
test3 2022-03-27
test4 2022-03-20
test4 2022-04-15
test4 2022-04-17
test5 2022-03-01 yes
test5 2022-03-04 yes
test5 2022-03-06 yes
test5 2022-03-12 yes
test5 2022-04-04 yes
test5 2022-04-10
test5 2022-04-14
test5 2022-05-04
test6 2022-03-05 yes
test6 2022-03-15 yes
test6 2022-06-20
test6 2022-06-24
test6 2022-06-27
This is my attempt:
df['old_data'] = np.where(df.groupby('name').cumcount().ge(4), 'yes', '')