1

I have the following table:

ColumnA ColumnB ColumnC
1 AB001 TYPE-A
2 AB012 TYPE-A
3 AB035 TYPE-B
4 AB039 TYPE-B
5 AB065 TYPE-A
6 AB088 TYPE-B

I should get the output such that, if ColumnC is TYPE-A then it should save as a separate column (ColumnD) with output www.website.com/abc/AB001 if it is TYPE-B then it should be www.website.com/xyz/AB035. The output table should look like the following:

Output_should_look_like_this

How do I do that?

1
  • How looks added column? Commented Mar 16, 2021 at 8:42

1 Answer 1

1

Use Series.map by dictionary for types strings and join together by +:

#if no match `TYPE-A` or `TYPE-B` added default value no match
s = df['ColumnC'].map({'TYPE-A':'abc','TYPE-B':'xyz'}).fillna('no match')
df['ColumnD'] = ' www.website.com/' + s + '/' + df['ColumnB'].astype(str)
print (df)
   ColumnA ColumnB ColumnC                     ColumnD
0        1   AB001  TYPE-A   www.website.com/abc/AB001
1        2   AB012  TYPE-A   www.website.com/abc/AB012
2        3   AB035  TYPE-B   www.website.com/xyz/AB035
3        4   AB039  TYPE-B   www.website.com/xyz/AB039
4        5   AB065  TYPE-A   www.website.com/abc/AB065
5        6   AB088  TYPE-B   www.website.com/xyz/AB088
Sign up to request clarification or add additional context in comments.

2 Comments

@jezael Thank for the help. If I try to implement I het the following error TypeError: can only concatenate str (not "int") to str.
@MuSu18 - Change df['ColumnB'] to df['ColumnB'].astype(str)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.