熊猫据帧复杂的计算
问题描述:
我有以下的数据帧,DF:熊猫据帧复杂的计算
Year totalPubs ActualCitations
0 1994 71 191.002034
1 1995 77 2763.911781
2 1996 69 2022.374474
3 1997 78 3393.094951
我想编写的代码,将做到以下几点:currentyear的
引文/总前两年
的totalPubs的我想要的东西,以创建一个名为影响因子的新列,并生成它,如下所示:
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年之和,我们再除以该值的引用值。