how to return return values based on another columns values in pandas?

I have the DF:

  col1|  col2|
   "a"| "stg"|
   "b"| "etc"|
   "a"| "xyz"|
   "b"| "wha"|

And I need the next output:

  col1|  col2|   col3|
   "a"| "stg"|"stg:1"|
   "b"| "etc"|"etc:2"|
   "a"| "xyz"|"xyz:1"|
   "b"| "wha"|"wha:2"|

So basically I have two columns with only string as variables What i need is third column, which examines col1. If it is an ‘a’ it returns col2.value + ‘:1’ and if a b it return col2.value + ‘:2’ as one single string I tried to write a function where

def return_stg(x):
    if df[df[col2] == x][col1] == "a":
        return x + ':1'
    if df[df[col2] == x][col1] == "b":
        return x + ':2'
df[col3] = df[col2].apply(return_stg)

but it throws a ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

but if I check it by typing only df[df[col2] == “stg”][col1] == “a” it returns a True, so I don’t see how it is ambiguous

basically I need that go for every row, check the value in col1, if it is an ‘a’, return the value from col2 concatenated with string ‘:1’ and etc

Answer

One solution:

df["col3"] = np.where(df["col1"] == "a", df["col2"] + ':1', df["col2"] + ':2')

Output:

 col1   col2     col3
0   a    stg    stg:1
1   b    etc    etc:2
2   a    xyz    xyz:1
3   b    wha    wha:2

First answer, slower (see comments) :

In the code you propose, inside the function return_stg(), you refer to the complete dataset df instead of the current row x (so you are comparing a dataset to a single value). This is what throws the ValueError. I would correct it like this:

def return_stg(x):
    if x["col1"] == "a":
        return x["col2"] + ':1'
    else:
        return x["col2"] + ':2'
df["col3"] = df.apply(return_stg, axis=1)

You can write it in one line:

df["col3"] = df.apply(lambda x: x["col2"] + ':1' if x["col1"] == "a" else x["col2"] + ':2', axis=1)