【发布时间】:2020-11-09 17:07:03
【问题描述】:
我有一个学术期刊数据集。变量Top Journal 是一个虚拟变量,如果论文发表在顶级期刊上,则等于 1。
Publication Month 是论文发表的数字月份。 author1、author2 等是该行具体论文的作者。
对于每个作者,我想计算之前在顶级期刊上发表的文章数量。因此,我想计算他/她的名字在authorX 之一列中的所有先前出现次数,但仅限于论文发表在顶级期刊上时。
df = pd.DataFrame({'Top Journal': [1,0,1],
'Publication Year': [2020, 2020, 2020],
'Publication Month': [8,8,7],
'author1': ['Hendren, Nathaniel', 'Backus, Matthew','Enke, Benjamin'],
'author2': ['Sprung-Keyser, Ben', 'Blake, Thomas', 'Hendren, Nathaniel'],
'author3': [None,'Larsen, Brad', None ]},
index = ['UID1', 'UID2', 'UID3'])
输出应如下所示:
Top Publication Publication author1 author2 author3 previous_publications1 previous_publications2 previous_publications3
Journal Year Month
1 2020 8 Hendren, Nathaniel Sprung-Keyser, Ben None 1 0 None
0 2020 8 Backus, Matthew Blake, Thomas Larsen, Brad 0 0 0
1 2020 7 Enke, Benjamin Hendren, Nathaniel None 0 0 None
重要提示:如果作者姓名在author1 中被提及一次,它可能会出现在另一个观察中的任何其他位置(例如author6)。
以前的顶级期刊出版物的数量应该显示在新列previous_publications1、previous_publications2,其中数字指的是各自的作者。因此,与 Hendren, Nathaniel 第二次出现在第三行时相比,第一篇论文 (Hendren, Nathaniel) 的作者 1 的发表次数更多。
【问题讨论】:
-
您能否发布您的预期输出?
-
当然,抱歉。例如,因为只有“Hendren, Nathaniel”出现在另一个顶级期刊中,所以前三行的附加列将如下所示:
previous_publications1: 1 0 0previous_publications2: 0 0 0previous_publications3: 无 0 无 -
假设有一个名为
df的数据框以及 df.index 并将代码复制并粘贴到您的问题中,您能做到df.to_dict()吗? -
你是这个意思吗?
标签: python pandas dataframe conditional-statements countif