基本上,我的数据帧是这样的:
id | refers
----------------
1 | [2,3]
2 | [1,3]
3 | []
我想添加另一列,显示另一个id引用该id的次数。例如:
id | refers | referred_count
----------------------------------
1 | [2,3] | 1
2 | [1,3] | 1
3 | [] | 2
我当前的代码如下所示:
citations_dict = {}
for index, row in data_ref.iterrows():
if len(row['reference_list']) > 0:
for reference in row['reference_list']:
if reference not in citations_dict:
citations_dict[reference] = {}
d = data_ref.loc[data_ref['id'] == reference]
citations_dict[reference]['venue'] = d['venue']
citations_dict[reference]['reference'] = d['reference']
citations_dict[reference]['citation'] = 1
else:
citations_dict[reference]['citation'] += 1
问题是,这个代码需要很长时间。我想知道如何做得更快,也许用熊猫?你知道吗
数据
创建引用出现次数的字典:
用每个id中的refer\计数减去refer\计数:
输出:
步骤1:获取reference列中每个ID的计数并将其存储在字典中,并在创建新列时应用该函数。你知道吗
输出
我想这正是你需要的!你知道吗
首先使用^{} 和^{} 创建一个助手
Series
。你知道吗这将是以
id
作为索引的列“refered\u count”的值。你知道吗然后您可以将df的
reset_index
转换为id
以便于本系列的合并,最后reset_index
以使数据帧恢复到原始形状。你知道吗[输出]
相关问题 更多 >
编程相关推荐