Skip to content

Commit 100af99

Browse files
committed
Update 602. Friend Requests II.py
1 parent e3f6c92 commit 100af99

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import pandas as pd
2+
3+
def most_friends(requests: pd.DataFrame) -> pd.DataFrame:
4+
# Create a DataFrame that contains all friend relationships in both directions
5+
friend_df = pd.concat([
6+
requests[['requester_id', 'accepter_id']].rename(columns={'requester_id': 'id', 'accepter_id': 'friend'}),
7+
requests[['accepter_id', 'requester_id']].rename(columns={'accepter_id': 'id', 'requester_id': 'friend'})
8+
])
9+
10+
# Count number of friends for each user
11+
friend_counts = friend_df.groupby('id').size().reset_index(name='num')
12+
13+
# Get the user with the most friends
14+
max_friends = friend_counts.loc[friend_counts['num'].idxmax()]
15+
16+
return pd.DataFrame({'id': [max_friends['id']], 'num': [max_friends['num']]})

0 commit comments

Comments
 (0)