大熊猫数据帧复杂计算

时间:2015-06-22 10:38:13

标签: python python-2.7 pandas dataframe

我有以下数据框,df:

     Year  totalPubs  ActualCitations
0   1994         71       191.002034
1   1995         77      2763.911781
2   1996         69      2022.374474
3   1997         78      3393.094951

我想编写可执行以下操作的代码:

当前年份的引用/前两年总数的总和

我想要创建一个名为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'])

1 个答案:

答案 0 :(得分:1)

我相信以下是您想要的:

imageView

因此,上面使用rolling_sumshift来生成前两年的总和,然后我们将引用值除以该值。