I have two pandas DataFrames :
df1 = pd.DataFrame({'user_id':['0','0','1','1','2','3','3'],
'friend_id':['1','2','3','2','4','4','5'],
'date_sent':['01-01-2020','01-01-2020','01-02-2020','01-03-2020','01-02-2020','01-03-2020','01-02-2020'],
'date_accepted':['01-01-2020','01-01-2020','01-02-2020',None,'01-10-2020',None,'01-21-2020']})
df2 = pd.DataFrame({'user_id':['1','1','2','2','3','3'],
'page_liked':['A','B','A','C','B','D']})
grouped1 = df1.groupby(['user_id','friend_id']).count()
grouped2 = df2.groupby(['user_id','page_liked']).count()
print(grouped1)
output >>>
date_sent date_accepted
user_id friend_id
0 1 1 1
2 1 1
1 2 1 0
3 1 1
2 4 1 1
3 4 1 0
5 1 1
grouped2
output >>>
user_id page_liked
1 A
B
2 A
C
3 B
D
I am trying to merge grouped1.friend_id with grouped2.user_id. The goal would be to obtain the following table:
user_id friend_id page_liked
0 1 A
B
2 A
C
1 2 A
C
3 B
D
2 4 Na
3 4 Na
5 Na
I've tried doing merge in multiple ways with no luck since the indices are multi level. I have also tried grouped1.combine_first(grouped2) but this seems to only work when the index levels are the same, so I am stuck at the moment.
grouped2into your IDE. For some reason when you doprint(grouped2)it does not print anything, and that is most likely because the data frame is only an index.