I have a dataframe with a column of lists ('value') and a column with the count of items in the respective list ('no_of_values') sorted descendingly by the no_of_values.
df=pd.DataFrame({'value':[['AB','BC','CD','DE','EF','FG','GH','HI'],
['BC','CD','DE','IJ','JK','KL','LM'],
['AB','CD','DE','IJ','JK','GH','HI'],
['AB','CD','DE','MN'],
['C', 'D', 'M'],
['MN','NO'],
['APQ']],
'no_of_values': [8,7,7,4,3,2,1]})
Now I want to count how many items in the value lists, going from the highest no_of_values to the lowest.
I tried:
df['no_of_1st_occurence'] = (~df['value'].explode().duplicated()).groupby(level=0).sum()
Which results in:
value no_of_values no_of_1st_occurence
0 [AB, BC, CD, DE, EF, FG, GH, HI] 8 8
1 [BC, CD, DE, IJ, JK, KL, LM] 7 4
2 [AB, CD, DE, IJ, JK, GH, HI] 7 0
3 [AB, CD, DE, MN] 4 1
4 [C, D, M] 3 3
5 [MN, NO] 2 1
6 [APQ] 1 1
But this does not work, when a no_of_values occurs for more than one time (here: "7"). I would like to split the count of 1st_occurences in that case into strict no_of_1st_occurence (if an item in the value list did not occur for a higher no_of_values nor the same no_of_values) and shared_1st_occurences (if an item in the value list did not occur for a higher no_of_values, but occurs atleast another times for the same no_of_values).
The desired outcome would be:
value no_of_values | strict_1st_occurence | shared_1st_occurence
0 [AB, BC, CD, DE, EF, FG, GH, HI] 8 8 0
1 [BC, CD, DE, IJ, JK, KL, LM] 7 2 2
2 [AB, CD, DE, IJ, JK, GH, HI] 7 0 2
3 [AB, CD, DE, MN] 4 1 0
4 [C, D, M] 3 3 0
5 [MN, NO] 2 1 0
6 [APQ] 1 1 0
