I want to merge 2 columns of the same dataframe, but by using some specific condition.
consider the following dataframe :
| number-first | Number-second |
|---|---|
| 1 | Nan |
| 2 | 4C |
| 3A | 5 |
| Nan | 6 |
| Nan | 7 |
| Nan | Nan |
The conditions are:
- If the Number-first column has a alphanumeric value and the Number-second Column has a Nan value or a '' (empty string) -> the Result column should only consider the value from Number-first
- If the Number-first column has a Nan or '' (empty string) value and the Number-second Column has a alphanumeric value -> the Result column should only consider the value from Number-second
- If the values from both the columns are alphanumeric the result column should consist of value from Number-first and Number-second, which are separated by a '-'
- If both the Columns have Nan or empty string values, the result should consist of a '' (empty string) value
Following would be the output for the above dataframe:
| Number-first | Number-second | Result |
|---|---|---|
| 1 | Nan | 1 |
| 2 | 4C | 2 - 4C |
| 3A | 5 | 3A - 5 |
| Nan | 6 | 6 |
| Nan | 7 | 7 |
| Nan | Nan | Nan |
I have been unsuccessful using the .select method and providing the above conditions.
Thanks in advance for the help !
below is the code snippet of the conditions, which don't seem to work for me:
conditions = [
df['Number-first'].str.isalnum(),
df['Number-second'].str.isalnum(),
df['Number-first'].str.isalnum() & df['Number-second'].str.isalnum() ]