我有以下数据帧df:
Year totalPubs ActualCitations
0 1994 71 191.002034
1 1995 77 2763.911781
2 1996 69 2022.374474
3 1997 78 3393.094951
我想写的代码会做以下事情:
当年引文/前两年的Pub总数之和
我想创建一个名为Impact Factor的新列,并按如下方式生成它:
for index, row in df.iterrows():
if row[0]>=1996:
df.at[index,'Impact Factor'] = df.at[index, 'ActualCitations'] / (df.at[index-1, 'totalPubs'] + df.at[index-2, 'totalPubs'])
我相信以下内容可以满足您的需求:
In [24]:
df['New_Col'] = df['ActualCitations']/pd.rolling_sum(df['totalPubs'].shift(), window=2)
df
Out[24]:
Year totalPubs ActualCitations New_Col
0 1994 71 191.002034 NaN
1 1995 77 2763.911781 NaN
2 1996 69 2022.374474 13.664692
3 1997 78 3393.094951 23.240376
因此,上面使用rolling_sum
和shift
来生成前2年的总和,然后我们将引用值除以该值。