I want to find the relationship between elements of different yet connected data frames. Here I have a df that shows friendships: id1 is friends with id2, id3, id4, id5, id21 etc.
| friend1 | friend2 | |
|---|---|---|
| row1 | id1 | id3 |
| row2 | id2 | id1 |
| row3 | id5 | id1 |
| row4 | id12 | id2 |
| row5 | id21 | id1 |
| row6 | id4 | id2 |
| row7 | id7 | id8 |
| row8 | id1 | id4 |
| row9 | id21 | id2 |
| row10 | id3 | id5 |
Here is another dataframe where it shows when someone goes to a party. For example, Id5 went to parties on 2012-02-03 and 2012-05-09.
| person | date | |
|---|---|---|
| row1 | id1 | 2012-02-03 |
| row2 | id2 | 2012-05-09 |
| row3 | id5 | 2012-02-03 |
| row4 | id12 | 2012-05-09 |
| row5 | id21 | 2012-02-03 |
| row6 | id7 | 2012-02-22 |
| row7 | id5 | 2012-05-09 |
| row8 | id3 | 2012-02-22 |
| row9 | id8 | 2012-02-22 |
| row10 | id1 | 2012-02-22 |
I want to find the correlation between people attending parties depending on whether their friends attend. For example for id1:
Went to party 2012-02-03 (same day as id21, id5) and 2012-02-22 (same day as id7, id3, id8). So 2 friends on 1 occasion and 1 on another (mean=1.5 friends when he attends a party).
I would like to see the average number of friends existing at a party for each person present in the dataset. If someone has no friends, visited no parties, or visited parties without his friends then the mean will be 0.
I tried to build this using pandas methods like value_counts/groupby and dictionaries but I lost hope along the way. Thanks in advance for any help.
Here are the constructors for the dfs:
index = ['r1', 'r2', 'r3', 'r4', 'r5', 'r6', 'r7', 'r8', 'r9', 'r10']
data1 = {'friend1': ['id1', 'id2','id5','id12','id21','id4','id7','id1','id21', 'id3'],
'friend2': ['id3', 'id1','id1','id2','id1','id2','id8','id4','id2','id5']}
data2 = {'person': ['id1', 'id2','id5','id12','id21','id7','id5','id3','id8','id1'],
'date': ['2012-02-03', '2012-05-09', '2012-02-03', '2012-05-09', '2012-02-03','2012-02-22','2012-05-09','2012-02-22','2012-02-22','2012-02-22']}
df1 = pd.DataFrame(data1, index=index)
df2 = pd.DataFrame(data2, index=index)