gpt4 book ai didi

python - Pandas Dataframe 复杂计算

转载 作者:太空宇宙 更新时间:2023-11-04 05:49:21 24 4
gpt4 key购买 nike

我有以下数据框,df:

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

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

当前年份的引用/前两年的 totalPubs 之和

我想要一些东西来创建一个名为 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_sumshift生成前 2 年的总和,然后我们将引用值除以该值。

关于python - Pandas Dataframe 复杂计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30977816/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com