I have a DataFrame that looks like this (but 149110 rows instead):
df = {'group':['a','a','a','a',
'b','b','b','b','b','b','b','b','b',
'c','c','c','c','c',
'd','d','d','d','d','d','d',
'e','e','e','e',],
'date':[np.datetime64('2020-01-01'),np.datetime64('2020-01-01'),np.datetime64('2020-01-01'),np.datetime64('2020-01-01'),
np.datetime64('2019-03-12'),np.datetime64('2019-03-12'),np.datetime64('2019-03-12'),np.datetime64('2019-03-12'),
np.datetime64('2019-03-12'),np.datetime64('2019-03-12'),np.datetime64('2019-03-12'),np.datetime64('2019-03-12'),
np.datetime64('2019-03-12'),
np.datetime64('2020-01-01'),np.datetime64('2020-01-01'),np.datetime64('2020-01-01'),np.datetime64('2020-01-01'),
np.datetime64('2020-01-01'),
np.datetime64('2019-01-17'),np.datetime64('2019-01-17'),np.datetime64('2019-01-17'),np.datetime64('2019-01-17'),
np.datetime64('2019-01-17'),np.datetime64('2019-01-17'),np.datetime64('2019-01-17'),
np.datetime64('2018-12-03'),np.datetime64('2018-12-03'),np.datetime64('2018-12-03'),np.datetime64('2018-12-03')],
'id':['tom','taliha','alyssa','randyl',
'tom','taliha','edward','aaron','daniel','jean','sigmund','albus','riddle',
'fellicia','ron','fred','george','alex',
'taliha','alyssa','locke','jon','jamie','sam','sydney',
'jon','jamie','sam','arya'],
'value':[1,2,3,4,
7,6,4,8,2,3,5,9,1,
1,2,3,4,5,
5,7,6,3,4,1,2,
3,2,1,4]}
df= pd.DataFrame(df)
df
group date id value
0 a 2020-01-01 tom 1
1 a 2020-01-01 taliha 2
2 a 2020-01-01 alyssa 3
3 a 2020-01-01 randyl 4
4 b 2019-03-12 tom 7
5 b 2019-03-12 taliha 6
6 b 2019-03-12 edward 4
7 b 2019-03-12 aaron 8
8 b 2019-03-12 daniel 2
9 b 2019-03-12 jean 3
10 b 2019-03-12 sigmund 5
11 b 2019-03-12 albus 9
12 b 2019-03-12 riddle 1
13 c 2020-01-01 fellicia1
14 c 2020-01-01 ron 2
15 c 2020-01-01 fred 3
16 c 2020-01-01 george 4
17 c 2020-01-01 alex 5
18 d 2019-01-17 taliha 5
19 d 2019-01-17 alyssa 7
20 d 2019-01-17 locke 6
21 d 2019-01-17 jon 3
22 d 2019-01-17 jamie 4
23 d 2019-01-17 sam 1
24 d 2019-01-17 sydney 2
25 e 2018-12-03 jon 3
26 e 2018-12-03 jamie 2
27 e 2018-12-03 sam 1
28 e 2018-12-03 arya 4
I need a column: together that returns 1 if the person has been with in a group with another person in the current group but in the past year.
For example in group 'a' we have 4 people, but tom and taliha were both in a group together in np.datetime64('2016-03-12') i.e. they were both in group 'b' together. We can also see that in group 'c' taliha and alyssa were also together. So I want the corresponding value of together for group a to have a 1 next to both tom, taliha and alyssa, but 0 for randyl because he hasn't been in a group with anyone else in the past year.
Then for group 'b' and 'c' because no one has been in a group with anyone else in the past year I want the value of together to be 0 for everyone.
For group 'd' within the last year we can see that jon, jamie and sam were in the same group i.e. they were part of group 'e'. So the value of together for jon, jamie and sam in group 'd' should be a 1 and the rest of the people a 0.
And as there were no data before group 'e', they should all be assigned 0.
Then I want to create another new column: rel based on this depending on the value that the people had in the pervious group. I want rel to be equal to 1 if that person in the past group had a value lower than the other person and to be -1 if their value was higher.
For example in group 'a' the value of rel for tom should be -1 because he had a higher value than taliha in 'b' and because of this the value of rel for taliha should be 1, because she had a lower value than tom in 'b'. For alyssa I want the value of rel to be set to -1 because in group d she has a higher value than talhia.
Basically the Idea is that the lower the value the better. I am trying to rank people by their past value. So for group 'a' I need a system that basically shows that
taliha > tom and taliha > alyssa. But we don't know the relationship between tom and alyssa so I have them as the same value. I also don't know the relationship between randyl and everyone else in the group 'a' so I want the value of rel set to 0 for him.
If for example I find the relationship like this: person 1 > person 2 > person 3 and no history for person 4. I want to have rel to reflect his relationship. I want the value of rel to look a little something like this person 1 = 2, person 2 = 0, person 3 = -2 and person 4 = 0.
So I want the resulting DataFrame to look something like this:
group date id value together rel
0 a 2020-01-01 tom 1 1 -1
1 a 2020-01-01 taliha 2 1 1
2 a 2020-01-01 alyssa 3 1 -1
3 a 2020-01-01 randyl 4 0 0
4 b 2019-03-12 tom 7 0 0
5 b 2019-03-12 taliha 6 0 0
6 b 2019-03-12 edward 4 0 0
7 b 2019-03-12 aaron 8 0 0
8 b 2019-03-12 daniel 2 0 0
9 b 2019-03-12 jean 3 0 0
10 b 2019-03-12 sigmund 5 0 0
11 b 2019-03-12 albus 9 0 0
12 b 2019-03-12 riddle 1 0 0
13 c 2020-01-01 fellicia1 0 0
14 c 2020-01-01 ron 2 0 0
15 c 2020-01-01 fred 3 0 0
16 c 2020-01-01 george 4 0 0
17 c 2020-01-01 alex 5 0 0
18 d 2019-01-17 taliha 5 0 0
19 d 2019-01-17 alyssa 7 0 0
20 d 2019-01-17 locke 6 0 0
21 d 2019-01-17 jon 3 1 -2
22 d 2019-01-17 jamie 4 1 0
23 d 2019-01-17 sam 1 1 2
24 d 2019-01-17 sydney 2 0 0
25 e 2018-12-03 jon 3 0 0
26 e 2018-12-03 jamie 2 0 0
27 e 2018-12-03 sam 1 0 0
28 e 2018-12-03 arya 4 0 0