May-15-2018, 05:33 AM
Hi All,
Need your help on one issue.
I have a dataframe 'df' which has 3 columns of 'object' data type- opt1,opt2,opt3
Now I need to create a new column 'var' which will have followings-
1. if opt2 and opt 3 are None/Null/Empty and opt1 is not None/Null/Empty:
then df[var] = df[opt1]
2. elif opt1 and opt 3 are None/Null/Empty and opt2 is None/Null/Empty:
then df[var] = df[opt1] + '|' + df[opt3]
3. elif opt1 and opt 2 are None/Null/Empty and opt3 is None/Null/Empty:
then df[var] = df[opt1] + '|' + df[opt2]
4. else: df[var] = df[opt1] + '|' + df[opt2] + '|' + df[opt3]
Please suggest me to put above conditions in Python 3.6 or share me any better approach.
Thanks!
Need your help on one issue.
I have a dataframe 'df' which has 3 columns of 'object' data type- opt1,opt2,opt3
Now I need to create a new column 'var' which will have followings-
1. if opt2 and opt 3 are None/Null/Empty and opt1 is not None/Null/Empty:
then df[var] = df[opt1]
2. elif opt1 and opt 3 are None/Null/Empty and opt2 is None/Null/Empty:
then df[var] = df[opt1] + '|' + df[opt3]
3. elif opt1 and opt 2 are None/Null/Empty and opt3 is None/Null/Empty:
then df[var] = df[opt1] + '|' + df[opt2]
4. else: df[var] = df[opt1] + '|' + df[opt2] + '|' + df[opt3]
Please suggest me to put above conditions in Python 3.6 or share me any better approach.
Thanks!