consider the dataframe df
np.random.seed([3,1415])
df = pd.DataFrame(np.random.choice(a, (5, 10))).add_prefix('col ')
print(df)
col 0 col 1 col 2 col 3 col 4 col 5 col 6 col 7 col 8 col 9
0 Q L C K P X N L N T
1 I X A W Y M W A C A
2 U Z H T N S M E D T
3 N W H X N U F D X F
4 Z L Y H M G E H W S
Construct a custom format function
f = lambda row: '{col 1}:{col 2}-{col 3}({col 4})'.format(**row)
And apply to df
df.astype(str).apply(f, 1)
0 L:C-K(P)
1 W:A-C(A)
2 W:H-X(N)
3 E:H-W(S)
4 Y:E-P(N)
dtype: object
Add a new column with assign
df.assign(New=df.astype(str).apply(f, 1))
# assign in place with
# df['New'] = df.astype(str).apply(f, 1)
col 0 col 1 col 2 col 3 col 4 col 5 col 6 col 7 col 8 col 9 New
0 Q L C K P X N L N T L:C-K(P)
1 I X A W Y M W A C A X:A-W(Y)
2 U Z H T N S M E D T Z:H-T(N)
3 N W H X N U F D X F W:H-X(N)
4 Z L Y H M G E H W S L:Y-H(M)
Or you can wrap this up into another function that operates on pd.Series. This requires that you pass the columns in the correct order.
def u(a, b, c, d):
return a + ':' + b + '-' + c + '(' + d + ')'
df.assign(New=u(df['col 1'], df['col 2'], df['col 3'], df['col 4']))
# assign in place with
# df['New'] = u(df['col 1'], df['col 2'], df['col 3'], df['col 4'])
col 0 col 1 col 2 col 3 col 4 col 5 col 6 col 7 col 8 col 9 New
0 Q L C K P X N L N T L:C-K(P)
1 I X A W Y M W A C A X:A-W(Y)
2 U Z H T N S M E D T Z:H-T(N)
3 N W H X N U F D X F W:H-X(N)
4 Z L Y H M G E H W S L:Y-H(M)
cols[0],cols[1],cols[2]andcols[5]are strings. If not, you need to convert them to strings before combining them. In standard python code you would do this withstr(cols[0]). With pandas columns, you can do this withcols[0].astype(str).