python,python-2.7,pandas,dataframes , Pandas Dataframe Complex Calculation

## Question:

Tag: python,python-2.7,pandas,dataframes

I have the following 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
``````

I want to write code that would do the following:

Citations of currentyear / Sum of totalPubs of the two previous years

I want something to create a new column called Impact Factor, and generate it as follows:

``````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'])
``````

I believe the following does what you want:

``````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
``````

So the above uses `rolling_sum` and `shift` to generate the previous 2 years sum and we then divide the citations value by that value.

# Related:

## Peewee: reducing where conditionals break after a certain length

python,peewee
This is what I have: SomeTable.select.where(reduce(operator.or_, (SomeTable.stuff == entry for entry in big_list))) The problem arises when I have a relatively large list of elements in big_list and I get this: RuntimeError: maximum recursion depth exceeded Is there another way to approach this that doesn't involve splitting up the list...

## Replace nodejs for python?

python,node.js,webserver
i'm working in a HTML5 multiplayer game, and i need a server to sync player's movement, chat, battles, etc. So I'm looking for ways to use python instead nodejs, because i have I have more familiarity with python. The server is simple: var express = require('express'); var app = express();...

## SyntaxError: invalid syntax?

python,syntax
Good afternoon, I am developing a script in python and while I am trying to compile it from the terminator/terminal i always get this error, but I cannot understand where is the syntax error? File "_case1.py", line 128 print ('########################') ^ SyntaxError: invalid syntax Then I just change the position...

## Sort when values are None or empty strings python

python,list,sorting,null
I have a list with dictionaries in which I sort them on different values. I'm doing it with these lines of code: def orderBy(self, col, dir, objlist): if dir == 'asc': sorted_objects = sorted(objlist, key=lambda k: k[col]) else: sorted_objects = sorted(objlist, key=lambda k: k[col], reverse=True) return sorted_objects Now the problem...

## How do I read this list and parse it?

python,list
I'm using requests and the output I get from the sites API is a list, I've been stuck trying to parse it to get the data from it. I use r = requests.get(urlas, params=params) r.json() to get the data I want. Here is a snippet of the list [{'relation_type': None,...

## group indices of list in list of lists

python,list
I am looking for an elegant solution for the following problem. I have a list of ints and I want to create a list of lists where the indices with the same value are grouped together in the order of the occurrences of said list. [2, 0, 1, 1, 3,...