jp0rk
jp0rk

Reputation: 3

Pandas - combine some values within a column with another column

New to python and I have been trying to find a solution here (close, but not exactly the same scenario).

Let's say I have 2 columns:
ID1 = ['ABC12a', 'ABC12b', 'ABC12c'] and ID2 = ['123','234','345']

I want to merge these 2 columns into 1, but only retain partial characters of column ID1 (ID1[:3]) and all of column ID2.

So the end result should look like:
ID1 = ['ABC123','ABC234','ABC345']

I tried something like:

df['ID1'] = df['ID1'].apply(lambda x: x.replace(x[:3],str(df['ID2'])))

and this did not work at all...any help is super appreciated! thanks

Upvotes: 0

Views: 46

Answers (1)

hirolau
hirolau

Reputation: 13901

df['ID1'] = df['ID1'].str[0:3] + df['ID2']

Upvotes: 1

Related Questions