首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

使用pandas统计关系表中的相互关注人数

可以通过以下步骤实现:

  1. 导入pandas库并读取关系表数据:
代码语言:txt
复制
import pandas as pd

data = pd.read_csv('关系表.csv')
  1. 创建一个新的DataFrame来存储相互关注人数的统计结果:
代码语言:txt
复制
mutual_followers = pd.DataFrame(columns=['User', 'Mutual Followers'])
  1. 遍历关系表中的每一行,统计每个用户的相互关注人数:
代码语言:txt
复制
for index, row in data.iterrows():
    user = row['User']
    followers = row['Followers']
    following = row['Following']
    
    mutual_count = len(set(followers) & set(following))
    
    mutual_followers = mutual_followers.append({'User': user, 'Mutual Followers': mutual_count}, ignore_index=True)
  1. 对统计结果进行排序,以便找出相互关注人数最多的用户:
代码语言:txt
复制
mutual_followers = mutual_followers.sort_values(by='Mutual Followers', ascending=False)
  1. 打印出相互关注人数最多的用户及其相互关注人数:
代码语言:txt
复制
print(mutual_followers.head())

这样就可以使用pandas统计关系表中的相互关注人数了。

关于pandas的更多信息和使用方法,可以参考腾讯云的产品介绍链接:腾讯云-数据分析与AI-数据处理与分析-Pandas

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券