I want to create a new columns conditional on two other columns in python.
Below is the dataframe:
| name | address |
|---|---|
| apple | hello1234 |
| banana | happy111 |
| apple | str3333 |
| pie | diary5144 |
I want to create a new column "want", conditional on column "name" and "column" address. The rules are as follows: (1)If the value in "name" is apple, the the value in "want" should be the first five letters in column "address". (2)If the value in "name" is banana, the the value in "want" should be the first four letters in column "address". (3)If the value in "name" is pie, the the value in "want" should be the first three letters in column "address".
The dataframe I want look like this:
| name | address | want |
|---|---|---|
| apple | hello1234 | hello |
| banana | happy111 | happ |
| apple | str3333 | str33 |
| pie | diary5144 | dia |
How to address such problem? Thanks!
