I have the following problem:
Given a dataframe, say for example,
import pandas as pd
df = pd.DataFrame({'col1':[1,0,0,1],'col2':['B','B','A','A'],'col3':[1,2,3,4]})
In some other tool I can easily create a new column based on a condition, say
Create new column 'col3' with 'col2' if df['col1'] == '0' & ~df['col2'].isnull() else 'col1'
This other tool works it out pretty fast. I did not find a corresponding expression in python so far.
1.) I tried np.where which iterates through rows but does not allow dynamic values in the result corresponding to the exact row
2.) I've tried .apply(lambda ... ) which appears to be quiet slow.
I would be happy if you could find an elegant way to fix this problem. Thanx.