I have a dataframe that looks like this:
dict1 = {'category': {0: 0.0, 1: 1.0, 2: 0.0, 3: 1.0, 4: 0.0},
'Id': {0: 24108, 1: 24307, 2: 24307, 3: 24411, 4: 24411},
'count': {0: 3, 1: 2, 2: 33, 3: 98, 4: 33},
'weight': {0: 0.5, 1: 0.2, 2: 0.7, 3: 1.2, 4: 0.39}}
df1 = pd.DataFrame(dict1)
category Id count weight
0 0.0 24108 3 0.50
1 1.0 24307 2 0.20
2 0.0 24307 33 0.70
3 1.0 24411 98 1.20
4 0.0 24411 33 0.39
There are about 1000 such entries. Some only have 1 category tag, as is the example for Id 24108. Other Ids have two entries because there are two category tags, such as Id 24307 and 24411.
I want to generate a new column called val that follows one of 3 rules, depending on certain conditions.
If an Id only has 1 tag associated with it, such as Id 24108, then the value in the new column val should be a simple product between count and weight.
If an Id has 2 tags, such as Id 24307 or 24411, then the script should look at which one of the two entries has a larger count value first.
For Id 24307, the count for category 0 is higher than the count for category 1, therefore the val column should be 2 * (0.2+1) next to category 1 and 33 * (1-0.7) next to category 0
For Id 24411, the count for category 0 is lower than the count for category 1, therefore the val column should be 98 * (1-1.2) next to category 1 and 33 * (0.3+1) next to category 0.
The expected output should be like this:
category Id count weight val
0 0.0 24108 3 0.50 1.50
1 1.0 24307 2 0.20 2.40
2 0.0 24307 33 0.70 9.90
3 1.0 24411 98 1.20 -19.60
4 0.0 24411 33 0.39 42.90