Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

loop through dataframe and assign values based on previous row

import pandas as pd
test = pd.DataFrame({'Country':['USA','USA','USA','USA','USA'],
             'Month':[6,7,8,9,10],
              'Sales':[100,200,0,0,0],
              'Recovery':[0,1,1.5,2.5,3]
             })

test['Prediction'] = test['Sales']
for i in range(1, len(test)):
    #prevent division by zero
    if test.loc[i-1, 'Recovery'] != 0:
        test.loc[i, 'Prediction'] = test.loc[i-1, 'Prediction'] * test.loc[i, 'Recovery'] / test.loc[i-1, 'Recovery']
Comment

PREVIOUS NEXT
Code Example
Python :: ipython run script with command line arguments 
Python :: counter vectriozer in python 
Python :: rtdpy ncstr 
Python :: Command raised an exception: TypeError: discord.py 
Python :: python scroll 
Python :: converting 1d array into upper triangular 
Python :: list of thing same condition 
Python :: tkinter label abstand nach oben 
Python :: series clip 
Python :: factorielle python 
Python :: numpy how to apply interpolation all rows 
Python :: python return true for list comprehension 
Python :: python tkinter window size 
Python :: python ask for real values until negative value diplay highest and lowest 
Python :: django create view filter options 
Python :: Mat.at(row,col) Opencv 
Python :: def square_odd(pylist) 
Python :: python how to get variable value in dict 
Python :: what is mysoace 
Python :: equivalent of spread in R in python 
Python :: fibonacci using function in python 
Python :: duplicate characters in a string python 
Python :: xlabel font size python latex 
Python :: mongoengine ObjectIdField 
Python :: convert string to double 2 decimal places python 
Python :: check if set is a subset of another python 
Python :: append to a list without intializing 
Python :: how to simulate a keypress using pyautogui 
Python :: know functionality of any function using help 
Python :: pairplot hide original legend 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =