python - 在 pandas DataFrame 中计算 h-index(作者出版物的影响/生产力)的有效方法

标签 python pandas python-2.7 dataframe

我对 pandas 很陌生,但我一直在阅读有关它的文章以及它在处理大数据时的速度有多快。

我设法创建了一个数据框,现在我有了一个看起来像这样的 pandas 数据框:

    0     1
0    1    14
1    2    -1
2    3  1817
3    3    29
4    3    25
5    3     2
6    3     1
7    3    -1
8    4    25
9    4    24
10   4     2
11   4    -1
12   4    -1
13   5    25
14   5     1

第 0 列 是作者的 ID,第 1 列 是该作者在出版物中的引用次数(-1 表示零引用)。每行代表一位作者的不同出版物。

我正在尝试计算每个作者的 h-indexh-index 定义为作者拥有的 h 篇出版物中被引用至少 h 次的数量。所以对于作者:

author 1 has h-index of 1

author 2 has h-index of 0

author 3 has h-index of 3

author 4 has h-index of 2

author 5 has h-index of 1

这是我目前做的方式,其中涉及很多循环:

current_author=1
hindex=0

for index, row in df.iterrows():
    if row[0]==current_author:
        if row[1]>hindex:
            hindex+=1
    else:
        print "author ",current_author," has h-index:", hindex
        current_author+=1
        hindex=0
        if row[1]>hindex:
            hindex+=1
            
print "author ",current_author," has h-index:", hindex  

我的实际数据库有超过 300 万作者。如果我为每一个循环,这将需要几天的时间来计算。我想弄清楚您认为解决此问题的最快方法是什么?

提前致谢!

最佳答案

我在这里将您的列重命名为“作者”和“引用”,我们可以按作者分组,然后应用 lambda,这里的 lambda 将引用次数与值进行比较,如果为真,这将生成 1 或 0 ,然后我们可以这样总结:

In [104]:

df['h-index'] = df.groupby('author')['citations'].transform( lambda x: (x >= x.count()).sum() )
​
df
Out[104]:
    author  citations  h-index
0        1         14        1
1        2         -1        0
2        3       1817        3
3        3         29        3
4        3         25        3
5        3          2        3
6        3          1        3
7        3         -1        3
8        4         25        2
9        4         24        2
10       4          2        2
11       4         -1        2
12       4         -1        2
13       5         25        1
14       5          1        1

编辑 正如@Julien Spronck 所指出的,如果作者 4 有引用 3、3、3,则上述内容无法正常工作。通常您无法访问组间索引,但我们可以将引文值与 rank 进行比较,这是一个伪索引,但只有在引文值是唯一的情况下它才有效:

In [129]:

df['h-index'] = df.groupby('author')['citations'].transform(lambda x: ( x >= x.rank(ascending=False, method='first') ).sum() )
​
df
Out[129]:
    author  citations  h-index
0        1         14        1
1        2         -1        0
2        3       1817        3
3        3         29        3
4        3         25        3
5        3          2        3
6        3          1        3
7        3         -1        3
8        4         25        2
9        4         24        2
10       4          2        2
11       4         -1        2
12       4         -1        2
13       5         25        1
14       5          1        1

关于python - 在 pandas DataFrame 中计算 h-index(作者出版物的影响/生产力)的有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29671726/

相关文章:

python - Newick 树表示为 scipy.cluster.hierarchy 链接矩阵格式

python - 有没有更好的方法在Python中读取多个txt文件?

python - 如何使 cython 函数接受 float 或 double 数组输入?

Python pandas str. 从不干净的列中提取年份信息

python - Hive 数据到 Pandas 数据框

python - Python 中的 zip(*) 功能

python - 从差值 != 2 的列表中返回值

python - 将 describe 函数应用于 pandas groupby 时如何忽略行的最大值和最小值

python - 如何用Python制作一个像C++一样的计算器

python - 在 Python 模块中初始化静态列表的最佳方法