In the database 4 columns(A,B,C,D) are there. A,B and C column are used to group the column D. Based on A,B,C column , I want to concatenate D columns Id. Consider the below one is my database :
A B C D
A1 B1 C1 12
A1 B1 C1 15
A2 B2 C2 16
A4 B4 C4 18
A1 B1 C1 19
I am expecting the below result after running code:
A B C D
A1 B1 C1 12_15_19
A2 B2 C2 16
A4 B4 C4 18
I have used below code to perform this operation:
df23['combined']=df23.apply(lambda x:'%s_%s_%s' % (x['A'],x['B'],x['C']),axis=1)
for i in range(len(df23)):
df23['ABC'] = df23.iloc[:,3]
for j in range(len(df23)+1):
cur = df23.iloc[i,3]
nxt = df23.iloc[j,3]
if cur==nxt:
df23['ABC'] = df23.iloc[i,4] +'_'+ df23.iloc[j,3]
It is not working as per my expectation, can you please suggest me if any other way we can built for the same. Thanks in Advance:)