Assume the following Pandas df:
# Import dependency.
import pandas as pd
# Create data for df.
data = {'Value': [1000, 1020, 1011, 1010, 1030, 950, 1001, 1100, 1121, 1131],
'Dummy_Variable': [0,0,1,0,0,0,1,0,1,1]
}
# Create DataFrame
df = pd.DataFrame(data)
display(df)
I want to add a new column to the df called 'Placeholder.' The value of Placeholder would be based on the 'Dummy_Variable' column based on the following rules:
- If all previous rows had a 'Dummy_Variable' value of 0, then the 'Placeholder' value for that row would be equal to the 'Value' for that row.
- If the 'Dummy_Variable' value for a row equals 1, then the 'Placeholder' value for that row would be equal to the 'Value' for that row.
- If the 'Dummy_Variable' value for a row equals 0 but the 'Placeholder' value for the row immediately above it is >0, then the 'Placeholder' value for the row would be equal to the 'Placeholder' value for the row immediately above it.
The desired result is a df with new 'Placeholder' column that looks like the df generated by running the code below:
desired_data = {'Value': [1000, 1020, 1011, 1010, 1030, 950, 1001, 1100, 1121, 1131],
'Dummy_Variable': [0,0,1,0,0,0,1,0,1,1],
'Placeholder': [1000,1020,1011,1011,1011,1011,1001,1001,1121,1131]}
df1 = pd.DataFrame(desired_data)
display(df1)
I can do this easily in Excel, but I cannot figure out how to do it in Pandas without using a loop. Any help is greatly appreciated. Thanks!