I am trying to insert a row above specified rows every time a certain condition is met. Here is an example dataframe I created that is way less extensive than mine and the desired dataframe result.
So the goal is to insert np.NaN every time the 'Product' and 'Location' together are different. I have thought of .insert(), .iloc[], etc. but not sure how that input would even look if certain conditions are needed to be met. I have laid out the conditions to help visualize the conditional expectations as well:
df_no = pd.DataFrame({'Product': ['toy', 'toy', 'toy', 'toy', 'bear', 'bear'],
'Location': ['Dallas', 'Dallas', 'Houston', 'Houston', 'Miami', 'Miami'],
'Value' : [8, 7, 3, 5, 4, 7],
'Cumulative Value':[8, 15, 18, 23, 27, 34]})
df_no
Example of the conditions for me to proceed and sort of a poor attempt of code:
cond_Product = df_no['Product'] == df_no['Product'].shift(-1)
cond_Location = df_no['Location'] == df_no['Location'].shift(-1)
##df_no = np.where((cond_Product) & (cond_Location), np.NaN, df_no.value)
Expected result:
df_yes = pd.DataFrame({'Product': ['toy', 'toy', np.NaN, 'toy', 'toy', np.NaN, 'bear', 'bear'],
'Location': ['Dallas', 'Dallas', np.NaN, 'Houston', 'Houston', np.NaN, 'Miami', 'Miami'],
'Value' : [8, 7, np.NaN, 3, 5, np.NaN, 4, 7],
'Cumulative Value':[8, 15, np.NaN, 18, 23, np.NaN, 27, 34]})
df_yes