Using the values of a previous “row” in a pandas series
I have a CSV that looks like this (and when brought into a pandas Dataframe with read_csv() , it looks the same). I want to update the values in column ad_requests according to the following logic: For a given row, if ad_requests has a value, leave it alone. Else, give it a value of the previous row's value for ad_requests minus the previous row's value for impressions . So in the first example, we would like to end up with: I get partially there: df["ad_requests"] = [i if not pd.isnull(i) else ??? for i in df["ad_requests"]] And this is where I get stuck. After the else , I want to "go back"