I would like to copy the values from one column (PERSONAL INFORMATION) to the other (VARIABLE) based on multiple conditions as listed below
Check whether 'PERSONAL INFORMATION' column starts with digits
Check whether the corresponding row value in 'VARIABLE' column is Nan
Check whether the previous row value in 'VARIABLE' column is not Nan (Here 'gender' is not nan but there might be cases when it's Nan)
Once all the conditions are met, I would like to copy over the values from 'PERSONAL INFORMATION' COLUMN TO 'VARIABLE' COLUMN
Please find below how the input data looks like
df = pd.DataFrame({'PERSONAL INFORMATION':['Gender','1.Male','2.Female','Ethnicity','1.Chinese','2.Indian','3.Eurasian','Marital Status','1.Single','2.Married','3.Divorced'], 'VARIABLE':['gender', np.nan, np.nan,'ethn',np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan]})
Marital status options shouldn't be copied to "Variable" column as the previous row is Nan here.
I was using np.where option but wasn't sure how to check the previous row value. I don't wish to use for loop.
df['VARIABLE'] = np.where((df['PERSONAL
INFORMATION'].str.startswith(('\d+')) == True) & (df['VARIABLE'].isna() ==
True) & 3RD CONDITION FOR PREVIOUS ROW CHECK
Can you please help me to find out how to check the previous row value for nan. If it's Nan i don't wish to copy the data. If it's not Nan, then the data has to be copied